Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e909b32

Browse files
Create dumbbell-plots.md
1 parent 2d3714b commit e909b32

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

‎doc/python/dumbbell-plots.md‎

Lines changed: 176 additions & 0 deletions
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+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /