Skip to content

Commit 3912c21

Browse files
big batch of layout.geo docs
1 parent 37c8c81 commit 3912c21

12 files changed

+542
-63
lines changed

doc/python/axes.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
8+
format_version: "1.2"
99
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
@@ -22,7 +22,8 @@ jupyter:
2222
pygments_lexer: ipython3
2323
version: 3.7.3
2424
plotly:
25-
description: How to adjust axes properties in python. Includes examples of linear
25+
description:
26+
How to adjust axes properties in python. Includes examples of linear
2627
and logarithmic axes, axes titles, styling and coloring axes and grid lines,
2728
and more.
2829
display_as: file_settings
@@ -34,23 +35,24 @@ jupyter:
3435
thumbnail: thumbnail/axes.png
3536
---
3637

37-
This tutorial explain how to set the properties of Cartesian axes, namely [`go.layout.XAxis`](/python/reference/#layout-xaxis) and [`go.layout.YAxis`](python/reference/#layout-xaxis). Other kinds of axes are described in other tutorials:
38+
This tutorial explain how to set the properties of 2-dimensional Cartesian axes, namely [`go.layout.XAxis`](/python/reference/#layout-xaxis) and [`go.layout.YAxis`](python/reference/#layout-xaxis). Other kinds of axes are described in other tutorials:
39+
3840
- [3D axes](/python/3d-axes) The axis object is [`go.layout.Scene`](/python/reference/#layout-scene)
3941
- [Polar axes](/python/polar-chart/). The axis object is [`go.layout.Polar`](/python/reference/#layout-polar)
4042
- [Ternary axes](/python/ternary-plots). The axis object is [`go.layout.Ternary`](/python/reference/#layout-ternary)
41-
- For Geo axes, see [this choropleth example](/python/choropleth-maps/#world-choropleth-map). The axis object is [`go.layout.Geo`](/python/reference/#layout-geo)
42-
- For Mapbox axes, see the different mapbox tutorials in the [Maps page](/python/maps/). The axis object is [`go.layout.Mapbox`](/python/reference/#layout-mapbox)
43-
- Color axes are described in the [color scales tutorial](/python/colorscales/).
43+
- [Geo axes](/python/map-configuration/). The axis object is [`go.layout.Geo`](/python/reference/#layout-geo)
44+
- [Mapbox axes](/python/mapbox-layers/). The axis object is [`go.layout.Mapbox`](/python/reference/#layout-mapbox)
45+
- [Color axes](/python/colorscales/). The axis object is [`go.layout.Coloraxis`](/python/reference/#layout-coloraxis).
4446

4547
**See also** the tutorials on [subplots](/python/subplots) and [multiple axes](/python/multiple-axes/).
4648

47-
The different types of Cartesian axes are
49+
The different types of Cartesian axes are
50+
4851
- 'linear'
4952
- 'log' (see the [example below](#logarithmic-axes))
5053
- 'date' (see the [tutorial on timeseries](/python/time-series/))
5154
- 'category' (see for example [Bar Charts](/python/bar-charts/))
52-
- 'multicategory' (see the [example below](#subcategory-(multicategory)-axes) ))
53-
55+
- 'multicategory' (see the [example below](<#subcategory-(multicategory)-axes>))
5456

5557
#### Logarithmic Axes
5658

@@ -92,7 +94,7 @@ fig.show()
9294

9395
### Forcing an axis to be categorical
9496

95-
If you pass string values for the `x` or `y` parameter, plotly will automatically set the corresponding axis type to `category`, with the exception of string of numbers, in which case the axis is linear. It is however possible to force the axis type by setting explicitely `xaxis_type` to be `category`.
97+
If you pass string values for the `x` or `y` parameter, plotly will automatically set the corresponding axis type to `category`, with the exception of string of numbers, in which case the axis is linear. It is however possible to force the axis type by setting explicitely `xaxis_type` to be `category`.
9698

9799
```python
98100
import plotly.express as px
@@ -150,17 +152,16 @@ fig.update_layout(title_text="Multi-category axis",)
150152
fig.show()
151153
```
152154

153-
154155
#### Toggling Axes Lines, Ticks, Labels, and Autorange
155156

156-
The different groups of Cartesian axes properties are
157+
The different groups of Cartesian axes properties are
158+
157159
- tick values (locations of tick marks) and tick labels. Tick labels are placed at tick values.
158160
- lines: grid lines (passing through tick values), axis lines, zero lines
159161
- title of the axis
160162
- range of the axis
161163
- domain of the axis
162164

163-
164165
#### Tick Placement, Color, and Style
165166

166167
##### Toggling axis tick marks
@@ -169,7 +170,6 @@ Axis tick marks are disabled by default for the default `plotly` theme, but they
169170

170171
Here is an example of turning on inside x-axis and y-axis ticks in a faceted figure created using Plotly Express. Note how the `col` argument to `update_yaxes` is used to only turn on the y-axis ticks for the left-most subplot.
171172

172-
173173
```python
174174
import plotly.express as px
175175
df = px.data.iris()
@@ -265,7 +265,6 @@ fig.update_yaxes(showticklabels=False)
265265
fig.show()
266266
```
267267

268-
269268
##### Set axis label rotation and font
270269

271270
The orientation of the axis tick mark labels is configured using the `tickangle` axis property. The value of `tickangle` is the angle of rotation, in the clockwise direction, of the labels from vertical in units of degrees. The font family, size, and color for the tick labels are stored under the `tickfont` axis property.
@@ -324,7 +323,6 @@ fig.update_layout(title_text="Apple Stock Price")
324323
fig.show()
325324
```
326325

327-
328326
### Axis lines: grid and zerolines
329327

330328
##### Toggling Axis grid lines
@@ -534,7 +532,6 @@ fig.update_yaxes(range=[3, 9])
534532
fig.show()
535533
```
536534

537-
538535
### Fixed Ratio Axes
539536

540537
The `scaleanchor` and `scaleratio` axis properties can be used to force a fixed ratio of pixels per unit between two axes.
@@ -593,7 +590,7 @@ fig.show()
593590

594591
##### Decreasing the domain spanned by an axis
595592

596-
In the example below, the x and y axis are anchored together, and the range of the `xaxis` is set manually. By default, plotly extends the range of the axis (overriding the `range` parameter) to fit in the figure `domain`. You can restrict the `domain` to force the axis to span only the set range, by setting `constrain='domain'` as below.
593+
In the example below, the x and y axis are anchored together, and the range of the `xaxis` is set manually. By default, plotly extends the range of the axis (overriding the `range` parameter) to fit in the figure `domain`. You can restrict the `domain` to force the axis to span only the set range, by setting `constrain='domain'` as below.
597594

598595
```python
599596
import plotly.graph_objects as go
@@ -687,7 +684,7 @@ fig.show()
687684

688685
### Axis range for log axis type
689686

690-
If you are using a `log` type of axis and you want to set the range of the axis, you have to give the `log10` value of the bounds when using `fig.update_xaxes` or `fig.update_layout`. However, with `plotly.express` functions you pass directly the values of the range bounds (`plotly.express` then computes the appropriate values to pass to the figure layout).
687+
If you are using a `log` type of axis and you want to set the range of the axis, you have to give the `log10` value of the bounds when using `fig.update_xaxes` or `fig.update_layout`. However, with `plotly.express` functions you pass directly the values of the range bounds (`plotly.express` then computes the appropriate values to pass to the figure layout).
691688

692689
```python
693690
import plotly.express as px

doc/python/bubble-maps.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ jupyter:
3333
thumbnail: thumbnail/bubble-map.jpg
3434
---
3535

36+
#### Base Map Configuration
37+
38+
Plotly figures made with `px.scatter_geo`, `px.line_geo` or `px.choropleth` functions or containing `go.Choropleth` or `go.Scattergeo` graph objects have a `go.layout.Geo` object which can be used to [control the appearance of the base map](/python/map-configuration/) onto which data is plotted.
39+
3640
### Bubble map with Plotly Express
3741

3842
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/). With `px.scatter_geo`, each line of the dataframe is represented as a marker point. The column set as the `size` argument gives the size of markers.

doc/python/choropleth-maps.md

Lines changed: 152 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.1"
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
1313
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
1424
plotly:
1525
description: How to make choropleth maps in Python with Plotly.
1626
display_as: maps
@@ -23,14 +33,143 @@ jupyter:
2333
thumbnail: thumbnail/choropleth.jpg
2434
---
2535

26-
A [Choropleth Map](https://en.wikipedia.org/wiki/Choropleth_map) is a heatmap using geographical boundaries. It is used to represent spatial variations of a quantity. See also the [index of other geographical charts](/python/maps/).
36+
A [Choropleth Map](https://en.wikipedia.org/wiki/Choropleth_map) is a map composed of colored polygons. It is used to represent spatial variations of a quantity. This page documents how to build **outline** choropleth maps, but you can also build [choropleth **tile maps** using our Mapbox trace types](/python/mapbox-county-choropleth).
2737

28-
Below we show how to create Choropleth Maps using either Plotly Express' `px.choropleth` or the lower-level `go.Choropleth`.
38+
Below we show how to create Choropleth Maps using either Plotly Express' `px.choropleth` function or the lower-level `go.Choropleth` graph object.
39+
40+
#### Base Map Configuration
41+
42+
Plotly figures made with `px.scatter_geo`, `px.line_geo` or `px.choropleth` functions or containing `go.Choropleth` or `go.Scattergeo` graph objects have a `go.layout.Geo` object which can be used to [control the appearance of the base map](/python/map-configuration/) onto which data is plotted.
43+
44+
### Introduction: main parameters for choropleth outline maps
45+
46+
Making choropleth maps requires two main types of input:
47+
48+
1. Geometry information:
49+
1. This can either be a supplied GeoJSON file where each feature has either an `id` field or some identifying value in `properties`; or
50+
2. one of the built-in geometries within `plotly`: US states and world countries (see below)
51+
2. A list of values indexed by feature identifier.
52+
53+
The GeoJSON data is passed to the `geojson` argument, and the data is passed into the `color` argument of `px.choropleth_mapbox` (`z` if using `graph_objects`), in the same order as the IDs are passed into the `location` argument.
54+
55+
**Note** the `geojson` attribute can also be the URL to a GeoJSON file, which can speed up map rendering in certain cases.
2956

3057
### Choropleth Map with plotly.express
3158

3259
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/).
3360

61+
#### GeoJSON with `feature.id`
62+
63+
Here we load a GeoJSON file containing the geometry information for US counties, where `feature.id` is a [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code).
64+
65+
```python
66+
from urllib.request import urlopen
67+
import json
68+
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
69+
counties = json.load(response)
70+
71+
counties["features"][0]
72+
```
73+
74+
#### Data indexed by `id`
75+
76+
Here we load unemployment data by county, also indexed by [FIPS code](https://en.wikipedia.org/wiki/FIPS_county_code).
77+
78+
```python
79+
import pandas as pd
80+
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
81+
dtype={"fips": str})
82+
df.head()
83+
```
84+
85+
### Choropleth map using GeoJSON
86+
87+
**Note** In this example we set `layout.geo.scope` to `usa` to automatically configure the map to display USA-centric data in an appropriate projection. See the [Geo map configuration documentation](/python/map-configuration/) for an explanation.
88+
89+
```python
90+
from urllib.request import urlopen
91+
import json
92+
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
93+
counties = json.load(response)
94+
95+
import pandas as pd
96+
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
97+
dtype={"fips": str})
98+
99+
import plotly.express as px
100+
101+
fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
102+
color_continuous_scale="Viridis",
103+
range_color=(0, 12),
104+
scope="usa",
105+
labels={'unemp':'unemployment rate'}
106+
)
107+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
108+
fig.show()
109+
```
110+
111+
### Indexing by GeoJSON Properties
112+
113+
If the GeoJSON you are using either does not have an `id` field or you wish you use one of the keys in the `properties` field, you may use the `featureidkey` parameter to specify where to match the values of `locations`.
114+
115+
In the following GeoJSON object/data-file pairing, the values of `properties.district` match the values of the `district` column:
116+
117+
```python
118+
import plotly.express as px
119+
120+
df = px.data.election()
121+
geojson = px.data.election_geojson()
122+
123+
print(df["district"][2])
124+
print(geojson["features"][0]["properties"])
125+
```
126+
127+
To use them together, we set `locations` to `district` and `featureidkey` to `"properties.district"`. The `color` is set to the number of votes by the candidate named Bergeron.
128+
129+
**Note** In this example we set `layout.geo.visible` to `False` to hide the base map and frame, and we set `layout.geo.fitbounds` to `'locations'` to automatically zoom the map to show just the area of interest. See the [Geo map configuration documentation](/python/map-configuration/) for an explanation.
130+
131+
```python
132+
import plotly.express as px
133+
134+
df = px.data.election()
135+
geojson = px.data.election_geojson()
136+
137+
fig = px.choropleth(df, geojson=geojson, color="Bergeron",
138+
locations="district", featureidkey="properties.district",
139+
projection="mercator"
140+
)
141+
fig.update_geos(fitbounds="locations", visible=False)
142+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
143+
fig.show()
144+
```
145+
146+
### Discrete Colors
147+
148+
In addition to [continuous colors](/python/colorscales/), we can [discretely-color](/python/discrete-color/) our choropleth maps by setting `color` to a non-numerical column, like the name of the winner of an election.
149+
150+
**Note** In this example we set `layout.geo.visible` to `False` to hide the base map and frame, and we set `layout.geo.fitbounds` to `'locations'` to automatically zoom the map to show just the area of interest. See the [Geo map configuration documentation](/python/map-configuration/) for an explanation.
151+
152+
```python
153+
import plotly.express as px
154+
155+
df = px.data.election()
156+
geojson = px.data.election_geojson()
157+
158+
fig = px.choropleth(df, geojson=geojson, color="winner",
159+
locations="district", featureidkey="properties.district",
160+
projection="mercator", hover_data=["Bergeron", "Coderre", "Joly"]
161+
)
162+
fig.update_geos(fitbounds="locations", visible=False)
163+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
164+
fig.show()
165+
```
166+
167+
### Using Built-in Geometries
168+
169+
Plotly comes with two built-in geometries which do not require an external GeoJSON file: countries as defined in the Natural Earth dataset (see the "Cultural Base Map" section of the [Geo map configuration documentation](/python/map-configuration/) for an explanation and disclaimer) and US states.
170+
171+
To use the countries dataset, provide `locations` as [three-letter ISO country codes](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3).
172+
34173
```python
35174
import plotly.express as px
36175

@@ -42,6 +181,15 @@ fig = px.choropleth(df, locations="iso_alpha",
42181
fig.show()
43182
```
44183

184+
To use the USA states dataset, set `locationmode='USA-states'` and provide `locations` as two-letter state abbreviations:
185+
186+
```python
187+
import plotly.express as px
188+
189+
fig = px.choropleth(locations=["CA", "TX", "NY"], locationmode="USA-states", color=[1,2,3], scope="usa")
190+
fig.show()
191+
```
192+
45193
### Choropleth Maps with go.Choropleth
46194

47195
#### United States Choropleth Map

doc/python/filled-area-on-mapbox.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
8+
format_version: "1.1"
99
jupytext_version: 1.1.1
1010
kernelspec:
1111
display_name: Python 3
@@ -35,16 +35,16 @@ jupyter:
3535

3636
<!-- #region -->
3737

38-
### Mapbox Access Token
39-
40-
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.
38+
### Mapbox Access Token and Base Map Configuration
4139

40+
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.
4241

4342
There are three different ways to show a filled area in a Mapbox map:
43+
4444
1. Use a [Scattermapbox](https://plot.ly/python/reference/#scattermapbox) trace and set `fill` attribute to 'toself'
4545
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
4646
3. Use the [Choroplethmapbox](https://plot.ly/python/mapbox-county-choropleth/) trace type
47-
<!-- #endregion -->
47+
<!-- #endregion -->
4848

4949
### Filled `Scattermapbox` Trace
5050

@@ -140,4 +140,5 @@ fig.show()
140140
```
141141

142142
#### Reference
143+
143144
See https://plot.ly/python/reference/#scattermapbox for more information about mapbox and their attribute options.

0 commit comments

Comments
 (0)