Skip to content

Commit caaa5c0

Browse files
author
mahdis-z
committed
line and area on mapbox
1 parent 99d375c commit caaa5c0

File tree

2 files changed

+305
-0
lines changed

2 files changed

+305
-0
lines changed

python/Line-on-Mapbox.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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 a Mapbox Choropleth Map of US Counties in Python with
26+
Plotly.
27+
display_as: maps
28+
has_thumbnail: true
29+
ipynb: ~notebook_demo/56
30+
language: python
31+
layout: user-guide
32+
name: Mapbox Choropleth Maps
33+
order: 1
34+
page_type: example_index
35+
permalink: python/mapbox-county-choropleth/
36+
thumbnail: thumbnail/mapbox-choropleth.png
37+
title: Python Mapbox Choropleth Maps | plotly
38+
---
39+
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+
### How to draw a Line on a Map
47+
48+
To draw a line on your map, you either can use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) or [scattergeo](https://plot.ly/python/reference/#scattergeo) trace type. Then . In this example uses scattermapbox and define the drawing [mode](https://plot.ly/python/reference/#scattermapbox-mode) to the combination of markers and line.
49+
50+
```python
51+
import plotly.graph_objects as go
52+
53+
fig = go.Figure(go.Scattermapbox(
54+
mode = "markers+lines",
55+
lon = [10, 20, 30], lat = [10, 20,30],
56+
marker = {'size': 10}))
57+
58+
fig.add_trace(go.Scattermapbox(
59+
mode = "markers+lines",
60+
lon = [-50, -60,40], lat = [30, 10, -20],
61+
marker = {'size': 10}))
62+
63+
fig.update_layout(margin ={'l':0,'t':0,'b':0,'r':0},
64+
mapbox = {'center': {'lon': 10, 'lat': 10},
65+
'style': "stamen-terrain",
66+
'center': {'lon': -20, 'lat': -20},
67+
'zoom': 1})
68+
69+
fig.show()
70+
```
71+
72+
### Set Marker Symbols
73+
74+
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):
75+
- basic
76+
- streets
77+
- outdoors
78+
- light
79+
- dark
80+
- satellite
81+
- satellite-streets
82+
83+
```python
84+
import plotly.graph_objects as go
85+
86+
token = open(".mapbox_token").read() # you need your own token
87+
88+
fig = go.Figure(go.Scattermapbox(
89+
mode = "markers+text+lines",
90+
lon = [-75, -80, -50], lat = [45, 20, -20],
91+
marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]},
92+
text = ["Bus", "Harbor", "airport"],textposition = "bottom right"))
93+
94+
fig.update_layout(
95+
mapbox = {
96+
'accesstoken': token,
97+
'style': "outdoors", 'zoom': 0.7},
98+
showlegend = False)
99+
100+
fig.show()
101+
```
102+
### Contour Line on Glob
103+
This example uses [scattergeo](https://plot.ly/javascript/reference/#scattergeo) trace type to visualize the data as lines on a geographic map.
104+
105+
```python
106+
import plotly.graph_objects as go
107+
import pandas as pd
108+
109+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/globe_contours.csv')
110+
111+
scl = ['blue', 'yellow', 'pink', \
112+
'royalblue', 'fuchsia', 'blue', \
113+
'brown', 'purple', 'orange']
114+
115+
n_colors = len(scl)
116+
117+
fig = go.Figure()
118+
119+
for i, (lat, lon) in enumerate(zip(df.columns[::2], df.columns[1::2])):
120+
fig.add_trace(go.Scattergeo(
121+
lon = df[lon],
122+
lat = df[lat],
123+
mode = 'lines',
124+
line = dict(width = 2, color = scl[i % n_colors]
125+
)))
126+
127+
fig.update_layout(
128+
title_text = 'Contour lines over globe<br>(Click and drag to rotate)',
129+
showlegend = False,
130+
geo = dict(
131+
showland = True,
132+
showcountries = True,
133+
showocean = True,
134+
countrywidth = 0.5,
135+
landcolor = 'orange',
136+
lakecolor = 'cyan',
137+
oceancolor = 'cyan',
138+
projection = dict(
139+
type = 'orthographic',
140+
rotation = dict(
141+
lon = -100,
142+
lat = 40,
143+
roll = 0
144+
)
145+
),
146+
lonaxis = dict(
147+
showgrid = True,
148+
gridcolor = 'black',
149+
gridwidth = 0.5
150+
),
151+
lataxis = dict(
152+
showgrid = True,
153+
gridcolor = 'black',
154+
gridwidth = 0.5
155+
)
156+
)
157+
)
158+
159+
fig.show()
160+
```
161+
162+
#### Reference
163+
See https://plot.ly/python/reference/#choroplethmapbox for more information about mapbox and their attribute options.

python/Mapbox-area.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 a Mapbox Choropleth Map of US Counties in Python with
26+
Plotly.
27+
display_as: maps
28+
has_thumbnail: true
29+
ipynb: ~notebook_demo/56
30+
language: python
31+
layout: user-guide
32+
name: Mapbox Choropleth Maps
33+
order: 1
34+
page_type: example_index
35+
permalink: python/mapbox-county-choropleth/
36+
thumbnail: thumbnail/mapbox-choropleth.png
37+
title: Python Mapbox Choropleth Maps | plotly
38+
---
39+
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+
### How to Show an Area on a Map
47+
48+
There are three different ways to show an area in a mapbox:
49+
- Set `fill` attribute to 'toself'
50+
- Define the corresponding geojson
51+
- 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.
52+
53+
The following example uses `Scattermapbox` and sets `fill = 'toself'`
54+
55+
```python
56+
import plotly.graph_objects as go
57+
58+
fig = go.Figure(go.Scattermapbox(
59+
fill = "toself",
60+
lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45],
61+
marker = { 'size': 10, 'color': "orange" }))
62+
63+
fig.update_layout(
64+
mapbox = {
65+
'style': "stamen-terrain",
66+
'center': {'lon': -73, 'lat': 46 },
67+
'zoom': 5},
68+
showlegend = False)
69+
70+
fig.show()
71+
```
72+
73+
### Provide Gaps on Map
74+
75+
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.
76+
77+
```python
78+
import plotly.graph_objects as go
79+
80+
fig = go.Figure(go.Scattermapbox(
81+
mode = "lines", fill = "toself",
82+
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]))
83+
84+
fig.update_layout(
85+
mapbox = {'style': "stamen-terrain", 'center': {'lon': 30, 'lat': 30}, 'zoom': 2},
86+
showlegend = False,
87+
margin = {'l':0, 'r':0, 'b':0, 't':0})
88+
89+
fig.show()
90+
```
91+
92+
### Use the Coresponding Geojson
93+
94+
```python
95+
import plotly.graph_objects as go
96+
97+
fig = go.Figure(go.Scattermapbox(
98+
mode = "markers",
99+
lon = [-73.605], lat = [45.51],
100+
marker = {'size': 20, 'color': ["cyan"]}))
101+
102+
fig.update_layout(
103+
mapbox = {
104+
'style': "stamen-terrain",
105+
'center': { 'lon': -73.6, 'lat': 45.5},
106+
'zoom': 12, 'layers': [{
107+
'source': {
108+
'type': "FeatureCollection",
109+
'features': [{
110+
'type': "Feature",
111+
'geometry': {
112+
'type': "MultiPolygon",
113+
'coordinates': [[[
114+
[-73.606352888, 45.507489991], [-73.606133883, 45.50687600],
115+
[-73.605905904, 45.506773980], [-73.603533905, 45.505698946],
116+
[-73.602475870, 45.506856969], [-73.600031904, 45.505696003],
117+
[-73.599379992, 45.505389066], [-73.599119902, 45.505632008],
118+
[-73.598896977, 45.505514039], [-73.598783894, 45.505617001],
119+
[-73.591308727, 45.516246185], [-73.591380782, 45.516280145],
120+
[-73.596778656, 45.518690062], [-73.602796770, 45.521348046],
121+
[-73.612239983, 45.525564037], [-73.612422919, 45.525642061],
122+
[-73.617229085, 45.527751983], [-73.617279234, 45.527774160],
123+
[-73.617304713, 45.527741334], [-73.617492052, 45.527498362],
124+
[-73.617533258, 45.527512253], [-73.618074188, 45.526759105],
125+
[-73.618271651, 45.526500673], [-73.618446320, 45.526287943],
126+
[-73.618968507, 45.525698560], [-73.619388002, 45.525216750],
127+
[-73.619532966, 45.525064183], [-73.619686662, 45.524889290],
128+
[-73.619787038, 45.524770086], [-73.619925742, 45.524584939],
129+
[-73.619954486, 45.524557690], [-73.620122362, 45.524377961],
130+
[-73.620201713, 45.524298907], [-73.620775593, 45.523650879]
131+
]]]
132+
}
133+
}]
134+
},
135+
'type': "fill", 'below': "traces", 'color': "royalblue"}]},
136+
margin = {'l':0, 'r':0, 'b':0, 't':0})
137+
138+
fig.show()
139+
```
140+
141+
#### Reference
142+
See https://plot.ly/python/reference/#choroplethmapbox for more information about mapbox and their attribute options.

0 commit comments

Comments
 (0)