From f097c2d9fecbb47c0db6de7dd80b5b68591b9039 Mon Sep 17 00:00:00 2001 From: mahdis-z Date: Wed, 11 Sep 2019 12:54:53 -0400 Subject: [PATCH 1/2] line and area on mapbox --- python/filled-area-on-mapbox.md | 143 ++++++++++++++++++++++++++++++++ python/lines-on-mapbox.md | 131 +++++++++++++++++++++++++++++ python/scattermapbox.md | 31 +++++++ 3 files changed, 305 insertions(+) create mode 100644 python/filled-area-on-mapbox.md create mode 100644 python/lines-on-mapbox.md diff --git a/python/filled-area-on-mapbox.md b/python/filled-area-on-mapbox.md new file mode 100644 index 000000000..71630a68f --- /dev/null +++ b/python/filled-area-on-mapbox.md @@ -0,0 +1,143 @@ +--- +jupyter: + jupytext: + notebook_metadata_filter: all + text_representation: + extension: .md + format_name: markdown + format_version: '1.1' + jupytext_version: 1.2.1 + kernelspec: + display_name: Python 3 + language: python + name: python3 + language_info: + codemirror_mode: + name: ipython + version: 3 + file_extension: .py + mimetype: text/x-python + name: python + nbconvert_exporter: python + pygments_lexer: ipython3 + version: 3.7.3 + plotly: + description: How to make an area on Map in Python with Plotly. + display_as: maps + has_thumbnail: true + ipynb: ~notebook_demo/56 + language: python + layout: user-guide + name: Filled Area on Maps + order: 1 + page_type: example_index + permalink: python/filled-area-on-mapbox/ + thumbnail: thumbnail/area.jpg + title: Python Mapbox Choropleth Maps | plotly +--- + + +### Mapbox Access Token + +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. + + +### How to Show an Area on a Map + +There are three different ways to show an area in a mapbox: +- Use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and set `fill` attribute to 'toself' +- Use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and define the corresponding geojson +- 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. + +The following example uses `Scattermapbox` and sets `fill = 'toself'` + +```python +import plotly.graph_objects as go + +fig = go.Figure(go.Scattermapbox( + fill = "toself", + lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45], + marker = { 'size': 10, 'color': "orange" })) + +fig.update_layout( + mapbox = { + 'style': "stamen-terrain", + 'center': {'lon': -73, 'lat': 46 }, + 'zoom': 5}, + showlegend = False) + +fig.show() +``` + +### Provide Gaps on Map + +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. + +```python +import plotly.graph_objects as go + +fig = go.Figure(go.Scattermapbox( + mode = "lines", fill = "toself", + 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])) + +fig.update_layout( + mapbox = {'style': "stamen-terrain", 'center': {'lon': 30, 'lat': 30}, 'zoom': 2}, + showlegend = False, + margin = {'l':0, 'r':0, 'b':0, 't':0}) + +fig.show() +``` + +### Use the Corresponding Geojson + +The second way is using Scattermapbox trace with the corresponding geojson. + +```python +import plotly.graph_objects as go + +fig = go.Figure(go.Scattermapbox( + mode = "markers", + lon = [-73.605], lat = [45.51], + marker = {'size': 20, 'color': ["cyan"]})) + +fig.update_layout( + mapbox = { + 'style': "stamen-terrain", + 'center': { 'lon': -73.6, 'lat': 45.5}, + 'zoom': 12, 'layers': [{ + 'source': { + 'type': "FeatureCollection", + 'features': [{ + 'type': "Feature", + 'geometry': { + 'type': "MultiPolygon", + 'coordinates': [[[ + [-73.606352888, 45.507489991], [-73.606133883, 45.50687600], + [-73.605905904, 45.506773980], [-73.603533905, 45.505698946], + [-73.602475870, 45.506856969], [-73.600031904, 45.505696003], + [-73.599379992, 45.505389066], [-73.599119902, 45.505632008], + [-73.598896977, 45.505514039], [-73.598783894, 45.505617001], + [-73.591308727, 45.516246185], [-73.591380782, 45.516280145], + [-73.596778656, 45.518690062], [-73.602796770, 45.521348046], + [-73.612239983, 45.525564037], [-73.612422919, 45.525642061], + [-73.617229085, 45.527751983], [-73.617279234, 45.527774160], + [-73.617304713, 45.527741334], [-73.617492052, 45.527498362], + [-73.617533258, 45.527512253], [-73.618074188, 45.526759105], + [-73.618271651, 45.526500673], [-73.618446320, 45.526287943], + [-73.618968507, 45.525698560], [-73.619388002, 45.525216750], + [-73.619532966, 45.525064183], [-73.619686662, 45.524889290], + [-73.619787038, 45.524770086], [-73.619925742, 45.524584939], + [-73.619954486, 45.524557690], [-73.620122362, 45.524377961], + [-73.620201713, 45.524298907], [-73.620775593, 45.523650879] + ]]] + } + }] + }, + 'type': "fill", 'below': "traces", 'color': "royalblue"}]}, + margin = {'l':0, 'r':0, 'b':0, 't':0}) + +fig.show() +``` + +#### Reference +See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options. diff --git a/python/lines-on-mapbox.md b/python/lines-on-mapbox.md new file mode 100644 index 000000000..1066072c7 --- /dev/null +++ b/python/lines-on-mapbox.md @@ -0,0 +1,131 @@ +--- +jupyter: + jupytext: + notebook_metadata_filter: all + text_representation: + extension: .md + format_name: markdown + format_version: '1.1' + jupytext_version: 1.2.1 + kernelspec: + display_name: Python 3 + language: python + name: python3 + language_info: + codemirror_mode: + name: ipython + version: 3 + file_extension: .py + mimetype: text/x-python + name: python + nbconvert_exporter: python + pygments_lexer: ipython3 + version: 3.7.3 + plotly: + description: How to draw a line on Map in Python with Plotly. + display_as: maps + has_thumbnail: true + ipynb: ~notebook_demo/56 + language: python + layout: user-guide + name: Lines on Mapbox + order: 1 + page_type: example_index + permalink: python/lines-on-mapbox/ + thumbnail: thumbnail/line_mapbox.jpg + title: Lines on Mapbox | plotly +--- + +### Mapbox Access Token + +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. + +### How to draw a Line on a Map + +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. + +```python +import pandas as pd + +us_cities1 = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") +States = ['New York', 'Ohio'] +us_cities = us_cities1[us_cities1.State.isin(States)] + +import plotly.express as px + +fig = px.line_mapbox( + us_cities, + lat="lat", + lon="lon", + line_group='State', + hover_name="City", + hover_data=["State", "Population"], + color_discrete_sequence=["fuchsia"], + zoom=3, + height=300) + +fig.update_layout( + mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41, + margin={"r":0,"t":0,"l":0,"b":0}) + +fig.show() +``` + +This example uses scattermapbox and defines +the drawing [mode](https://plot.ly/python/reference/#scattermapbox-mode) to the combination of markers and line. + +```python +import plotly.graph_objects as go + +fig = go.Figure(go.Scattermapbox( + mode = "markers+lines", + lon = [10, 20, 30], + lat = [10, 20,30], + marker = {'size': 10})) + +fig.add_trace(go.Scattermapbox( + mode = "markers+lines", + lon = [-50, -60,40], + lat = [30, 10, -20], + marker = {'size': 10})) + +fig.update_layout( + margin ={'l':0,'t':0,'b':0,'r':0}, + mapbox = { + 'center': {'lon': 10, 'lat': 10}, + 'style': "stamen-terrain", + 'center': {'lon': -20, 'lat': -20}, + 'zoom': 1}) + +fig.show() +``` + +This example uses scattermapbox trace and shows how to customize hoverinfo in Mapbox. + +```python +import pandas as pd + +us_cities1 = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") +States = ['Washington'] +us_cities = us_cities1[us_cities1.State.isin(States)] + +import plotly.graph_objects as go + +fig = go.Figure(go.Scattermapbox( + lat=us_cities.lat, + lon=us_cities.lon, + mode='markers+lines', + marker={'color':'fuchsia', 'size':10, 'opacity':0.8}, + hovertext=us_cities['City'], + hoverinfo='lat+lon+text')) + +fig.update_layout( + mapbox={'style': 'stamen-terrain', 'center':{'lat':47, 'lon':-117}, 'zoom':5}, + margin={"r":0,"t":0,"l":0,"b":0}) + +fig.show() + +``` + +#### Reference +See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options. diff --git a/python/scattermapbox.md b/python/scattermapbox.md index a3dc400b7..071be60be 100644 --- a/python/scattermapbox.md +++ b/python/scattermapbox.md @@ -196,5 +196,36 @@ fig.update_layout( fig.show() ``` +### Set Marker Symbols + +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): +- basic +- streets +- outdoors +- light +- dark +- satellite +- satellite-streets + +```python +import plotly.graph_objects as go + +token = open(".mapbox_token").read() # you need your own token + +fig = go.Figure(go.Scattermapbox( + mode = "markers+text+lines", + lon = [-75, -80, -50], lat = [45, 20, -20], + marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]}, + text = ["Bus", "Harbor", "airport"],textposition = "bottom right")) + +fig.update_layout( + mapbox = { + 'accesstoken': token, + 'style': "outdoors", 'zoom': 0.7}, + showlegend = False) + +fig.show() +``` + #### Reference See https://plot.ly/python/reference/#scattermapbox for more information and options! From b78ebd9da42adfd761fbb61e503fcfe99c4b0515 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Tue, 8 Oct 2019 21:02:02 -0400 Subject: [PATCH 2/2] final tweaks --- python/filled-area-on-mapbox.md | 27 ++++++++------- python/lines-on-mapbox.md | 60 +++++++-------------------------- python/scattermapbox.md | 4 +-- 3 files changed, 29 insertions(+), 62 deletions(-) diff --git a/python/filled-area-on-mapbox.md b/python/filled-area-on-mapbox.md index 71630a68f..52e6ec2fe 100644 --- a/python/filled-area-on-mapbox.md +++ b/python/filled-area-on-mapbox.md @@ -6,7 +6,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.1' - jupytext_version: 1.2.1 + jupytext_version: 1.1.1 kernelspec: display_name: Python 3 language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.3 + version: 3.6.8 plotly: description: How to make an area on Map in Python with Plotly. display_as: maps @@ -36,18 +36,20 @@ jupyter: title: Python Mapbox Choropleth Maps | plotly --- + ### Mapbox Access Token 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. -### How to Show an Area on a Map +There are three different ways to show a filled area in a Mapbox map: +1. Use a [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and set `fill` attribute to 'toself' +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 +3. Use the [Choroplethmapbox](https://plot.ly/python/mapbox-county-choropleth/) trace type + -There are three different ways to show an area in a mapbox: -- Use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and set `fill` attribute to 'toself' -- Use [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and define the corresponding geojson -- 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. +### Filled `Scattermapbox` Trace The following example uses `Scattermapbox` and sets `fill = 'toself'` @@ -69,16 +71,17 @@ fig.update_layout( fig.show() ``` -### Provide Gaps on Map +### Multiple Filled Areas with a `Scattermapbox` trace -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. +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. ```python import plotly.graph_objects as go fig = go.Figure(go.Scattermapbox( mode = "lines", fill = "toself", - 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])) + lon = [-10, -10, 8, 8, -10, None, 30, 30, 50, 50, 30, None, 100, 100, 80, 80, 100], + lat = [30, 6, 6, 30, 30, None, 20, 30, 30, 20, 20, None, 40, 50, 50, 40, 40])) fig.update_layout( mapbox = {'style': "stamen-terrain", 'center': {'lon': 30, 'lat': 30}, 'zoom': 2}, @@ -88,9 +91,9 @@ fig.update_layout( fig.show() ``` -### Use the Corresponding Geojson +### GeoJSON Layers -The second way is using Scattermapbox trace with the corresponding geojson. +In this map we add a GeoJSON layer. ```python import plotly.graph_objects as go diff --git a/python/lines-on-mapbox.md b/python/lines-on-mapbox.md index 1066072c7..a42d08bcb 100644 --- a/python/lines-on-mapbox.md +++ b/python/lines-on-mapbox.md @@ -6,7 +6,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.1' - jupytext_version: 1.2.1 + jupytext_version: 1.1.1 kernelspec: display_name: Python 3 language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.3 + version: 3.6.8 plotly: description: How to draw a line on Map in Python with Plotly. display_as: maps @@ -40,39 +40,30 @@ jupyter: 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. -### How to draw a Line on a Map +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. -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. +### Lines on Mapbox maps using Plotly Express ```python import pandas as pd -us_cities1 = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") -States = ['New York', 'Ohio'] -us_cities = us_cities1[us_cities1.State.isin(States)] +us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") +us_cities = us_cities.query("State in ['New York', 'Ohio']") import plotly.express as px -fig = px.line_mapbox( - us_cities, - lat="lat", - lon="lon", - line_group='State', - hover_name="City", - hover_data=["State", "Population"], - color_discrete_sequence=["fuchsia"], - zoom=3, - height=300) +fig = px.line_mapbox(us_cities, lat="lat", lon="lon", color="State", zoom=3, height=300) -fig.update_layout( - mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41, +fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() ``` -This example uses scattermapbox and defines -the drawing [mode](https://plot.ly/python/reference/#scattermapbox-mode) to the combination of markers and line. +### Lines on Mapbox maps using `Scattermapbox` traces + +This example uses `go.Scattermapbox` and sets +the [mode](https://plot.ly/python/reference/#scattermapbox-mode) attribute to a combination of markers and line. ```python import plotly.graph_objects as go @@ -100,32 +91,5 @@ fig.update_layout( fig.show() ``` -This example uses scattermapbox trace and shows how to customize hoverinfo in Mapbox. - -```python -import pandas as pd - -us_cities1 = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv") -States = ['Washington'] -us_cities = us_cities1[us_cities1.State.isin(States)] - -import plotly.graph_objects as go - -fig = go.Figure(go.Scattermapbox( - lat=us_cities.lat, - lon=us_cities.lon, - mode='markers+lines', - marker={'color':'fuchsia', 'size':10, 'opacity':0.8}, - hovertext=us_cities['City'], - hoverinfo='lat+lon+text')) - -fig.update_layout( - mapbox={'style': 'stamen-terrain', 'center':{'lat':47, 'lon':-117}, 'zoom':5}, - margin={"r":0,"t":0,"l":0,"b":0}) - -fig.show() - -``` - #### Reference See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options. diff --git a/python/scattermapbox.md b/python/scattermapbox.md index 071be60be..d49a30905 100644 --- a/python/scattermapbox.md +++ b/python/scattermapbox.md @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.6.7 + version: 3.6.8 plotly: description: How to make scatter plots on Mapbox maps in Python. display_as: maps @@ -198,7 +198,7 @@ fig.show() ### Set Marker Symbols -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): +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: - basic - streets - outdoors