Skip to content

Commit d124170

Browse files
Merge pull request #3931 from plotly/docs-oct
Documentation updates for 5.11
2 parents b14f8ad + 79c49c1 commit d124170

File tree

6 files changed

+455
-14
lines changed

6 files changed

+455
-14
lines changed

doc/python/dumbbell-plots.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.3'
9+
jupytext_version: 1.14.1
10+
kernelspec:
11+
display_name: Python 3 (ipykernel)
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.8.0
24+
plotly:
25+
description: How to create dumbbell plots in Python with Plotly.
26+
display_as: basic
27+
language: python
28+
layout: base
29+
name: Dumbbell Plots
30+
order: 19
31+
page_type: example_index
32+
permalink: python/dumbbell-plots/
33+
thumbnail: thumbnail/dumbbell-plot.jpg
34+
---
35+
36+
## Basic Dumbbell Plot
37+
38+
39+
Dumbbell plots are useful for demonstrating change between two sets of data points, for example, the population change for a selection of countries for two different years
40+
41+
In this example, we compare life expectancy in 1952 with life expectancy in 2002 for countries in Europe.
42+
43+
```python
44+
import plotly.graph_objects as go
45+
from plotly import data
46+
47+
import pandas as pd
48+
49+
df = data.gapminder()
50+
df = df.loc[(df.continent == "Europe") & (df.year.isin([1952, 2002]))]
51+
52+
countries = (
53+
df.loc[(df.continent == "Europe") & (df.year.isin([2002]))]
54+
.sort_values(by=["lifeExp"], ascending=True)["country"]
55+
.unique()
56+
)
57+
58+
data = {"x": [], "y": [], "colors": [], "years": []}
59+
60+
for country in countries:
61+
data["x"].extend(
62+
[
63+
df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0],
64+
df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0],
65+
None,
66+
]
67+
)
68+
data["y"].extend([country, country, None]),
69+
data["colors"].extend(["green", "blue", "brown"]),
70+
data["years"].extend(["1952", "2002", None])
71+
72+
fig = go.Figure(
73+
data=[
74+
go.Scatter(
75+
x=data["x"],
76+
y=data["y"],
77+
mode="lines",
78+
marker=dict(
79+
color="grey",
80+
),
81+
),
82+
go.Scatter(
83+
x=data["x"],
84+
y=data["y"],
85+
mode="markers+text",
86+
marker=dict(
87+
color=data["colors"],
88+
size=10,
89+
),
90+
hovertemplate="""Country: %{y} <br> Life Expectancy: %{x} <br><extra></extra>""",
91+
),
92+
]
93+
)
94+
95+
fig.update_layout(
96+
title="Life Expectancy in Europe: 1952 and 2002",
97+
width=1000,
98+
height=1000,
99+
showlegend=False,
100+
)
101+
102+
fig.show()
103+
104+
```
105+
106+
## Dumbbell Plot with Arrow Markers
107+
108+
*Note: The `arrow`, `angleref`, and `standoff` properties used on the `marker` in this example are new in 5.11*
109+
110+
In this example, we add arrow markers to the plot. The first trace adds the lines connecting the data points and arrow markers.
111+
The second trace adds circle markers. On the first trace, we use `standoff=8` to position the arrow marker back from the data point.
112+
For the arrow marker to point directly at the circle marker, this value should be half the circle marker size.
113+
114+
```python
115+
import pandas as pd
116+
import plotly.graph_objects as go
117+
from plotly import data
118+
119+
df = data.gapminder()
120+
df = df.loc[(df.continent == "Europe") & (df.year.isin([1952, 2002]))]
121+
122+
countries = (
123+
df.loc[(df.continent == "Europe") & (df.year.isin([2002]))]
124+
.sort_values(by=["lifeExp"], ascending=True)["country"]
125+
.unique()
126+
)
127+
128+
data = {"x": [], "y": [], "colors": [], "years": []}
129+
130+
for country in countries:
131+
data["x"].extend(
132+
[
133+
df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0],
134+
df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0],
135+
None,
136+
]
137+
)
138+
data["y"].extend([country, country, None]),
139+
data["colors"].extend(["silver", "lightskyblue", "white"]),
140+
data["years"].extend(["1952", "2002", None])
141+
142+
fig = go.Figure(
143+
data=[
144+
go.Scatter(
145+
x=data["x"],
146+
y=data["y"],
147+
mode="markers+lines",
148+
marker=dict(
149+
symbol="arrow", color="black", size=16, angleref="previous", standoff=8
150+
),
151+
),
152+
go.Scatter(
153+
x=data["x"],
154+
y=data["y"],
155+
text=data["years"],
156+
mode="markers",
157+
marker=dict(
158+
color=data["colors"],
159+
size=16,
160+
),
161+
hovertemplate="""Country: %{y} <br> Life Expectancy: %{x} <br> Year: %{text} <br><extra></extra>""",
162+
),
163+
]
164+
)
165+
166+
fig.update_layout(
167+
title="Life Expectancy in Europe: 1952 and 2002",
168+
width=1000,
169+
height=1000,
170+
showlegend=False,
171+
)
172+
173+
174+
fig.show()
175+
176+
```

doc/python/legend.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jupyter:
88
format_version: '1.3'
99
jupytext_version: 1.14.1
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.8.8
23+
version: 3.8.0
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -189,6 +189,31 @@ fig.update_layout(legend=dict(
189189
fig.show()
190190
```
191191

192+
#### Horizontal Legend Entry Width
193+
194+
*New in 5.11*
195+
196+
Set the width of hozitonal legend entries by setting `entrywidth`. Here we set it to `70` pixels. Pixels is the default unit for `entrywidth`, but you can set it to be a fraction of the plot width using `entrywidthmode='fraction`.
197+
198+
```python
199+
import plotly.express as px
200+
201+
df = px.data.gapminder().query("year==2007")
202+
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
203+
size="pop", size_max=45, log_x=True)
204+
205+
fig.update_layout(legend=dict(
206+
orientation="h",
207+
entrywidth=70,
208+
yanchor="bottom",
209+
y=1.02,
210+
xanchor="right",
211+
x=1
212+
))
213+
214+
fig.show()
215+
```
216+
192217
#### Styling Legends
193218

194219
Legends support many styling options.

doc/python/mapbox-layers.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.3.1
8+
format_version: '1.3'
9+
jupytext_version: 1.14.1
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.8
23+
version: 3.8.0
2424
plotly:
2525
description: How to make Mapbox maps in Python with various base layers, with
2626
or without needing a Mapbox Access token.
@@ -192,6 +192,37 @@ fig.show()
192192

193193
See the example in the [plotly and datashader tutorial](/python/datashader).
194194

195+
196+
#### Setting Map Bounds
197+
198+
*New in 5.11*
199+
200+
Set bounds for a map to specify an area outside which a user interacting with the map can't pan or zoom. Here we set a maximum longitude of `-180`, a minimum longitude of `-50`, a maximum latitude of `90`, and a minimum latitude of `20`.
201+
202+
```python
203+
import plotly.express as px
204+
import pandas as pd
205+
206+
us_cities = pd.read_csv(
207+
"https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv"
208+
)
209+
210+
fig = px.scatter_mapbox(
211+
us_cities,
212+
lat="lat",
213+
lon="lon",
214+
hover_name="City",
215+
hover_data=["State", "Population"],
216+
color_discrete_sequence=["fuchsia"],
217+
zoom=3,
218+
height=300,
219+
)
220+
fig.update_layout(mapbox_style="open-street-map")
221+
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
222+
fig.update_layout(mapbox_bounds={"west": -180, "east": -50, "south": 20, "north": 90})
223+
fig.show()
224+
```
225+
195226
#### Reference
196227

197228
See https://plotly.com/python/reference/layout/mapbox/ for more information and options!

0 commit comments

Comments
 (0)