GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing plotly.com — Map configuration and styling on geo maps in Python Tile Maps vs Outline Maps¶Plotly supports two different kinds of maps: If your figure is created with a
Geo maps are outline-based maps. If your figure is created with a
Note: Plotly Express cannot create empty figures, so the examples below mostly create an "empty" map using Physical Base Maps¶Plotly Geo maps have a built-in base map layer composed of physical and cultural (i.e. administrative border) data. In Plotly.py 6.3 and later, the base map layer is created from the following sources:
In earlier versions of Plotly.py, the base map layer is based on Natural Earth data only. Plotly includes data from Natural Earth "as-is". This dataset draws boundaries of countries according to de facto status. See the Natural Earth page for more details. Various lines and area fills can be shown or hidden, and their color and line-widths specified. In the default In [1]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Here is a map with all physical features enabled and styled, at a larger-scale 1:50m resolution: In [2]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos( resolution=50, showcoastlines=True, coastlinecolor="RebeccaPurple", showland=True, landcolor="LightGreen", showocean=True, oceancolor="LightBlue", showlakes=True, lakecolor="Blue", showrivers=True, rivercolor="Blue" ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Disabling Base Maps¶In certain cases, such as large scale choropleth maps, the default physical map can be distracting. In this case the In [3]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos( visible=False, resolution=50, showlakes=True, lakecolor="Blue", showrivers=True, rivercolor="Blue" ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Cultural Base Maps¶In addition to physical base map features, a "cultural" base map is included which is composed of country borders and selected sub-country borders such as states. In Plotly.py 6.3 and later, this base map is created from UN data for country borders, and Natural Earth data for sub-country borders. In earlier versions of Plotly.py, this base map is based on Natural Earth data only. Plotly includes data from Natural Earth "as-is". This dataset draws boundaries of countries according to defacto status. See the Natural Earth page for more details. To create a map with your own cultural features please refer to our choropleth documentation. Here is a map with only cultural features enabled and styled, at a 1:50m resolution, which includes only country boundaries. See below for country sub-unit cultural base map features: In [4]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos( visible=False, resolution=50, showcountries=True, countrycolor="RebeccaPurple" ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Map Projections¶Geo maps are drawn according to a given map projection that flattens the Earth's roughly-spherical surface into a 2-dimensional space. In the following examples, we show the In [5]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos(projection_type="orthographic") fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() In [6]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos(projection_type="natural earth") fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Map projections can be rotated using the The map below uses all of these attributes to demonstrate the types of effect this can yield: In [7]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos( center=dict(lon=-30, lat=-30), projection_rotation=dict(lon=30, lat=30, roll=30), lataxis_range=[-50,20], lonaxis_range=[0, 200] ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Automatic Zooming or Bounds Fitting¶The Changed in 7.0: In [8]:
import plotly.express as px fig = px.line_geo(lat=[0,15,20,35], lon=[5,10,25,30]) fig.update_geos(fitbounds="locations") fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Limiting the Zoom Range¶New in 7.0
In [9]:
import plotly.express as px fig = px.line_geo(lat=[0,15,20,35], lon=[5,10,25,30]) fig.update_geos(projection=dict(scale=1, minscale=0.5, maxscale=4)) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Named Map Scopes and Country Sub-Units¶In addition, the named "scope" of a map defines a sub-set of the earth's surface to draw. Each scope has a default projection type, center and roll, as well as bounds, and certain scopes contain country sub-unit cultural layers certain resolutions, such as The available scopes are: In [10]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos( visible=False, resolution=50, scope="north america", showcountries=True, countrycolor="Black", showsubunits=True, subunitcolor="Blue" ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() The In [11]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos( visible=False, resolution=110, scope="usa", showcountries=True, countrycolor="Black", showsubunits=True, subunitcolor="Blue" ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Graticules (Latitude and Longitude Grid Lines)¶A graticule can be drawn using In [12]:
import plotly.graph_objects as go fig = go.Figure(go.Scattergeo()) fig.update_geos(lataxis_showgrid=True, lonaxis_showgrid=True) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Reference¶See https://plotly.com/python/reference/layout/geo/ for more information and chart attribute options! What About Dash?¶Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library. Learn about how to install Dash at https://dash.plot.ly/installation. Everywhere in this page that you see import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |