Skip to content

Commit e634a16

Browse files
Merge pull request #125 from plotly/py_mapbox_area_line
Py mapbox area line
2 parents 5071ea9 + b78ebd9 commit e634a16

File tree

3 files changed

+273
-1
lines changed

3 files changed

+273
-1
lines changed

python/filled-area-on-mapbox.md

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.1.1
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.8
24+
plotly:
25+
description: How to make an area on Map in Python with Plotly.
26+
display_as: maps
27+
has_thumbnail: true
28+
ipynb: ~notebook_demo/56
29+
language: python
30+
layout: user-guide
31+
name: Filled Area on Maps
32+
order: 1
33+
page_type: example_index
34+
permalink: python/filled-area-on-mapbox/
35+
thumbnail: thumbnail/area.jpg
36+
title: Python Mapbox Choropleth Maps | plotly
37+
---
38+
39+
<!-- #region -->
40+
41+
### Mapbox Access Token
42+
43+
To plot on Mapbox maps with Plotly you *may* need a Mapbox account and a public [Mapbox Access Token](https://www.mapbox.com/studio). See our [Mapbox Map Layers](/python/mapbox-layers/) documentation for more information.
44+
45+
46+
There are three different ways to show a filled area in a Mapbox map:
47+
1. Use a [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and set `fill` attribute to 'toself'
48+
2. Use a Mapbox layout (i.e. by minimally using an empty [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace) and add a GeoJSON layer
49+
3. Use the [Choroplethmapbox](https://plot.ly/python/mapbox-county-choropleth/) trace type
50+
<!-- #endregion -->
51+
52+
### Filled `Scattermapbox` Trace
53+
54+
The following example uses `Scattermapbox` and sets `fill = 'toself'`
55+
56+
```python
57+
import plotly.graph_objects as go
58+
59+
fig = go.Figure(go.Scattermapbox(
60+
fill = "toself",
61+
lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45],
62+
marker = { 'size': 10, 'color': "orange" }))
63+
64+
fig.update_layout(
65+
mapbox = {
66+
'style': "stamen-terrain",
67+
'center': {'lon': -73, 'lat': 46 },
68+
'zoom': 5},
69+
showlegend = False)
70+
71+
fig.show()
72+
```
73+
74+
### Multiple Filled Areas with a `Scattermapbox` trace
75+
76+
The following example shows how to use `None` in your data to draw multiple filled areas. Such gaps in trace data are unconnected by default, but this can be controlled via the [connectgaps](https://plot.ly/python/reference/#scattermapbox-connectgaps) attribute.
77+
78+
```python
79+
import plotly.graph_objects as go
80+
81+
fig = go.Figure(go.Scattermapbox(
82+
mode = "lines", fill = "toself",
83+
lon = [-10, -10, 8, 8, -10, None, 30, 30, 50, 50, 30, None, 100, 100, 80, 80, 100],
84+
lat = [30, 6, 6, 30, 30, None, 20, 30, 30, 20, 20, None, 40, 50, 50, 40, 40]))
85+
86+
fig.update_layout(
87+
mapbox = {'style': "stamen-terrain", 'center': {'lon': 30, 'lat': 30}, 'zoom': 2},
88+
showlegend = False,
89+
margin = {'l':0, 'r':0, 'b':0, 't':0})
90+
91+
fig.show()
92+
```
93+
94+
### GeoJSON Layers
95+
96+
In this map we add a GeoJSON layer.
97+
98+
```python
99+
import plotly.graph_objects as go
100+
101+
fig = go.Figure(go.Scattermapbox(
102+
mode = "markers",
103+
lon = [-73.605], lat = [45.51],
104+
marker = {'size': 20, 'color': ["cyan"]}))
105+
106+
fig.update_layout(
107+
mapbox = {
108+
'style': "stamen-terrain",
109+
'center': { 'lon': -73.6, 'lat': 45.5},
110+
'zoom': 12, 'layers': [{
111+
'source': {
112+
'type': "FeatureCollection",
113+
'features': [{
114+
'type': "Feature",
115+
'geometry': {
116+
'type': "MultiPolygon",
117+
'coordinates': [[[
118+
[-73.606352888, 45.507489991], [-73.606133883, 45.50687600],
119+
[-73.605905904, 45.506773980], [-73.603533905, 45.505698946],
120+
[-73.602475870, 45.506856969], [-73.600031904, 45.505696003],
121+
[-73.599379992, 45.505389066], [-73.599119902, 45.505632008],
122+
[-73.598896977, 45.505514039], [-73.598783894, 45.505617001],
123+
[-73.591308727, 45.516246185], [-73.591380782, 45.516280145],
124+
[-73.596778656, 45.518690062], [-73.602796770, 45.521348046],
125+
[-73.612239983, 45.525564037], [-73.612422919, 45.525642061],
126+
[-73.617229085, 45.527751983], [-73.617279234, 45.527774160],
127+
[-73.617304713, 45.527741334], [-73.617492052, 45.527498362],
128+
[-73.617533258, 45.527512253], [-73.618074188, 45.526759105],
129+
[-73.618271651, 45.526500673], [-73.618446320, 45.526287943],
130+
[-73.618968507, 45.525698560], [-73.619388002, 45.525216750],
131+
[-73.619532966, 45.525064183], [-73.619686662, 45.524889290],
132+
[-73.619787038, 45.524770086], [-73.619925742, 45.524584939],
133+
[-73.619954486, 45.524557690], [-73.620122362, 45.524377961],
134+
[-73.620201713, 45.524298907], [-73.620775593, 45.523650879]
135+
]]]
136+
}
137+
}]
138+
},
139+
'type': "fill", 'below': "traces", 'color': "royalblue"}]},
140+
margin = {'l':0, 'r':0, 'b':0, 't':0})
141+
142+
fig.show()
143+
```
144+
145+
#### Reference
146+
See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options.

python/lines-on-mapbox.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.1.1
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.8
24+
plotly:
25+
description: How to draw a line on Map in Python with Plotly.
26+
display_as: maps
27+
has_thumbnail: true
28+
ipynb: ~notebook_demo/56
29+
language: python
30+
layout: user-guide
31+
name: Lines on Mapbox
32+
order: 1
33+
page_type: example_index
34+
permalink: python/lines-on-mapbox/
35+
thumbnail: thumbnail/line_mapbox.jpg
36+
title: Lines on Mapbox | plotly
37+
---
38+
39+
### Mapbox Access Token
40+
41+
To plot on Mapbox maps with Plotly you *may* need a Mapbox account and a public [Mapbox Access Token](https://www.mapbox.com/studio). See our [Mapbox Map Layers](/python/mapbox-layers/) documentation for more information.
42+
43+
To draw a line on your map, you either can use [`px.line_mapbox()`](https://www.plotly.express/plotly_express/#plotly_express.line_mapbox) in plotly express, or [`Scattermapbox`](https://plot.ly/python/reference/#scattermapbox) traces. Below we show you how to draw a line on Mapbox using plotly express.
44+
45+
### Lines on Mapbox maps using Plotly Express
46+
47+
```python
48+
import pandas as pd
49+
50+
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
51+
us_cities = us_cities.query("State in ['New York', 'Ohio']")
52+
53+
import plotly.express as px
54+
55+
fig = px.line_mapbox(us_cities, lat="lat", lon="lon", color="State", zoom=3, height=300)
56+
57+
fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41,
58+
margin={"r":0,"t":0,"l":0,"b":0})
59+
60+
fig.show()
61+
```
62+
63+
### Lines on Mapbox maps using `Scattermapbox` traces
64+
65+
This example uses `go.Scattermapbox` and sets
66+
the [mode](https://plot.ly/python/reference/#scattermapbox-mode) attribute to a combination of markers and line.
67+
68+
```python
69+
import plotly.graph_objects as go
70+
71+
fig = go.Figure(go.Scattermapbox(
72+
mode = "markers+lines",
73+
lon = [10, 20, 30],
74+
lat = [10, 20,30],
75+
marker = {'size': 10}))
76+
77+
fig.add_trace(go.Scattermapbox(
78+
mode = "markers+lines",
79+
lon = [-50, -60,40],
80+
lat = [30, 10, -20],
81+
marker = {'size': 10}))
82+
83+
fig.update_layout(
84+
margin ={'l':0,'t':0,'b':0,'r':0},
85+
mapbox = {
86+
'center': {'lon': 10, 'lat': 10},
87+
'style': "stamen-terrain",
88+
'center': {'lon': -20, 'lat': -20},
89+
'zoom': 1})
90+
91+
fig.show()
92+
```
93+
94+
#### Reference
95+
See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options.

python/scattermapbox.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.6.8
2424
plotly:
2525
description: How to make scatter plots on Mapbox maps in Python.
2626
display_as: maps
@@ -196,5 +196,36 @@ fig.update_layout(
196196
fig.show()
197197
```
198198

199+
### Set Marker Symbols
200+
201+
You can define a symbol on your map by setting [symbol](https://plot.ly/python/reference/#scattermapbox-marker-symbol) attribute. This attribute only works on Mapbox-provided `style`s:
202+
- basic
203+
- streets
204+
- outdoors
205+
- light
206+
- dark
207+
- satellite
208+
- satellite-streets
209+
210+
```python
211+
import plotly.graph_objects as go
212+
213+
token = open(".mapbox_token").read() # you need your own token
214+
215+
fig = go.Figure(go.Scattermapbox(
216+
mode = "markers+text+lines",
217+
lon = [-75, -80, -50], lat = [45, 20, -20],
218+
marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]},
219+
text = ["Bus", "Harbor", "airport"],textposition = "bottom right"))
220+
221+
fig.update_layout(
222+
mapbox = {
223+
'accesstoken': token,
224+
'style': "outdoors", 'zoom': 0.7},
225+
showlegend = False)
226+
227+
fig.show()
228+
```
229+
199230
#### Reference
200231
See https://plot.ly/python/reference/#scattermapbox for more information and options!

0 commit comments

Comments
 (0)