Skip to content

Commit f097c2d

Browse files
mahdis-znicolaskruchten
mahdis-z
authored andcommitted
line and area on mapbox
1 parent 5071ea9 commit f097c2d

File tree

3 files changed

+305
-0
lines changed

3 files changed

+305
-0
lines changed

python/filled-area-on-mapbox.md

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.2.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.7.3
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+
40+
### Mapbox Access Token
41+
42+
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.
43+
44+
45+
### How to Show an Area on a Map
46+
47+
There are three different ways to show an area in a mapbox:
48+
- Use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and set `fill` attribute to 'toself'
49+
- Use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and define the corresponding geojson
50+
- Use the new trace type: [Choroplethmapbox](https://plot.ly/python/mapbox-county-choropleth/) for mapbox cases, or [Choropleth](https://plot.ly/python/choropleth-maps/) trace for non-mapbox ones.
51+
52+
The following example uses `Scattermapbox` and sets `fill = 'toself'`
53+
54+
```python
55+
import plotly.graph_objects as go
56+
57+
fig = go.Figure(go.Scattermapbox(
58+
fill = "toself",
59+
lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45],
60+
marker = { 'size': 10, 'color': "orange" }))
61+
62+
fig.update_layout(
63+
mapbox = {
64+
'style': "stamen-terrain",
65+
'center': {'lon': -73, 'lat': 46 },
66+
'zoom': 5},
67+
showlegend = False)
68+
69+
fig.show()
70+
```
71+
72+
### Provide Gaps on Map
73+
74+
The following example shows how to use missing values in your data to provide gap in your graph. To ignore the gap on your plot, take benefit of [connectorgaps](https://plot.ly/python/reference/#scattermapbox-connectgaps) attribute.
75+
76+
```python
77+
import plotly.graph_objects as go
78+
79+
fig = go.Figure(go.Scattermapbox(
80+
mode = "lines", fill = "toself",
81+
lon = [-10, -10, 8, 8, None, 30, 30, 50, 50, None, 100, 100, 80, 80], lat = [30, 6, 6, 30, None, 20, 30, 30, 20, None, 40, 50, 50, 40]))
82+
83+
fig.update_layout(
84+
mapbox = {'style': "stamen-terrain", 'center': {'lon': 30, 'lat': 30}, 'zoom': 2},
85+
showlegend = False,
86+
margin = {'l':0, 'r':0, 'b':0, 't':0})
87+
88+
fig.show()
89+
```
90+
91+
### Use the Corresponding Geojson
92+
93+
The second way is using Scattermapbox trace with the corresponding geojson.
94+
95+
```python
96+
import plotly.graph_objects as go
97+
98+
fig = go.Figure(go.Scattermapbox(
99+
mode = "markers",
100+
lon = [-73.605], lat = [45.51],
101+
marker = {'size': 20, 'color': ["cyan"]}))
102+
103+
fig.update_layout(
104+
mapbox = {
105+
'style': "stamen-terrain",
106+
'center': { 'lon': -73.6, 'lat': 45.5},
107+
'zoom': 12, 'layers': [{
108+
'source': {
109+
'type': "FeatureCollection",
110+
'features': [{
111+
'type': "Feature",
112+
'geometry': {
113+
'type': "MultiPolygon",
114+
'coordinates': [[[
115+
[-73.606352888, 45.507489991], [-73.606133883, 45.50687600],
116+
[-73.605905904, 45.506773980], [-73.603533905, 45.505698946],
117+
[-73.602475870, 45.506856969], [-73.600031904, 45.505696003],
118+
[-73.599379992, 45.505389066], [-73.599119902, 45.505632008],
119+
[-73.598896977, 45.505514039], [-73.598783894, 45.505617001],
120+
[-73.591308727, 45.516246185], [-73.591380782, 45.516280145],
121+
[-73.596778656, 45.518690062], [-73.602796770, 45.521348046],
122+
[-73.612239983, 45.525564037], [-73.612422919, 45.525642061],
123+
[-73.617229085, 45.527751983], [-73.617279234, 45.527774160],
124+
[-73.617304713, 45.527741334], [-73.617492052, 45.527498362],
125+
[-73.617533258, 45.527512253], [-73.618074188, 45.526759105],
126+
[-73.618271651, 45.526500673], [-73.618446320, 45.526287943],
127+
[-73.618968507, 45.525698560], [-73.619388002, 45.525216750],
128+
[-73.619532966, 45.525064183], [-73.619686662, 45.524889290],
129+
[-73.619787038, 45.524770086], [-73.619925742, 45.524584939],
130+
[-73.619954486, 45.524557690], [-73.620122362, 45.524377961],
131+
[-73.620201713, 45.524298907], [-73.620775593, 45.523650879]
132+
]]]
133+
}
134+
}]
135+
},
136+
'type': "fill", 'below': "traces", 'color': "royalblue"}]},
137+
margin = {'l':0, 'r':0, 'b':0, 't':0})
138+
139+
fig.show()
140+
```
141+
142+
#### Reference
143+
See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options.

python/lines-on-mapbox.md

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.2.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.7.3
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+
### How to draw a Line on a Map
44+
45+
To draw a line on your map, you either can use [line_mapbox](https://www.plotly.express/plotly_express/#plotly_express.line_mapbox) in plotly express, or [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) and [scattergeo](https://plot.ly/python/reference/#scattergeo) trace type in plotly. Below we show you how to draw a line on Mapbox using plotly express.
46+
47+
```python
48+
import pandas as pd
49+
50+
us_cities1 = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
51+
States = ['New York', 'Ohio']
52+
us_cities = us_cities1[us_cities1.State.isin(States)]
53+
54+
import plotly.express as px
55+
56+
fig = px.line_mapbox(
57+
us_cities,
58+
lat="lat",
59+
lon="lon",
60+
line_group='State',
61+
hover_name="City",
62+
hover_data=["State", "Population"],
63+
color_discrete_sequence=["fuchsia"],
64+
zoom=3,
65+
height=300)
66+
67+
fig.update_layout(
68+
mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41,
69+
margin={"r":0,"t":0,"l":0,"b":0})
70+
71+
fig.show()
72+
```
73+
74+
This example uses scattermapbox and defines
75+
the drawing [mode](https://plot.ly/python/reference/#scattermapbox-mode) to the combination of markers and line.
76+
77+
```python
78+
import plotly.graph_objects as go
79+
80+
fig = go.Figure(go.Scattermapbox(
81+
mode = "markers+lines",
82+
lon = [10, 20, 30],
83+
lat = [10, 20,30],
84+
marker = {'size': 10}))
85+
86+
fig.add_trace(go.Scattermapbox(
87+
mode = "markers+lines",
88+
lon = [-50, -60,40],
89+
lat = [30, 10, -20],
90+
marker = {'size': 10}))
91+
92+
fig.update_layout(
93+
margin ={'l':0,'t':0,'b':0,'r':0},
94+
mapbox = {
95+
'center': {'lon': 10, 'lat': 10},
96+
'style': "stamen-terrain",
97+
'center': {'lon': -20, 'lat': -20},
98+
'zoom': 1})
99+
100+
fig.show()
101+
```
102+
103+
This example uses scattermapbox trace and shows how to customize hoverinfo in Mapbox.
104+
105+
```python
106+
import pandas as pd
107+
108+
us_cities1 = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")
109+
States = ['Washington']
110+
us_cities = us_cities1[us_cities1.State.isin(States)]
111+
112+
import plotly.graph_objects as go
113+
114+
fig = go.Figure(go.Scattermapbox(
115+
lat=us_cities.lat,
116+
lon=us_cities.lon,
117+
mode='markers+lines',
118+
marker={'color':'fuchsia', 'size':10, 'opacity':0.8},
119+
hovertext=us_cities['City'],
120+
hoverinfo='lat+lon+text'))
121+
122+
fig.update_layout(
123+
mapbox={'style': 'stamen-terrain', 'center':{'lat':47, 'lon':-117}, 'zoom':5},
124+
margin={"r":0,"t":0,"l":0,"b":0})
125+
126+
fig.show()
127+
128+
```
129+
130+
#### Reference
131+
See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options.

python/scattermapbox.md

+31
Original file line numberDiff line numberDiff line change
@@ -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 tiles (not work on raster tiles):
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)