Skip to content

Commit 69134eb

Browse files
committed
Update dumbbell-plots.md
1 parent 282e610 commit 69134eb

File tree

1 file changed

+60
-33
lines changed

1 file changed

+60
-33
lines changed

doc/python/dumbbell-plots.md

+60-33
Original file line numberDiff line numberDiff line change
@@ -55,47 +55,59 @@ countries = (
5555
.unique()
5656
)
5757

58-
data = {"x": [], "y": [], "colors": [], "years": []}
58+
data = {"line_x": [], "line_y": [], "1952": [], "2002": [], "colors": [], "years": [], "countries": []}
5959

6060
for country in countries:
61-
data["x"].extend(
61+
data["1952"].extend([df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0]])
62+
data["2002"].extend([df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0]])
63+
data["line_x"].extend(
6264
[
6365
df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0],
6466
df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0],
6567
None,
6668
]
6769
)
68-
data["y"].extend([country, country, None]),
69-
data["colors"].extend(["green", "blue", "brown"]),
70-
data["years"].extend(["1952", "2002", None])
70+
data["line_y"].extend([country, country, None]),
7171

7272
fig = go.Figure(
7373
data=[
7474
go.Scatter(
75-
x=data["x"],
76-
y=data["y"],
75+
x=data["line_x"],
76+
y=data["line_y"],
7777
mode="lines",
78+
showlegend=False,
7879
marker=dict(
79-
color="grey",
80-
),
80+
color='grey'
81+
)
8182
),
8283
go.Scatter(
83-
x=data["x"],
84-
y=data["y"],
85-
mode="markers+text",
84+
x=data["1952"],
85+
y=countries,
86+
mode="markers",
87+
name="1952",
8688
marker=dict(
87-
color=data["colors"],
88-
size=10,
89-
),
90-
hovertemplate="""Country: %{y} <br> Life Expectancy: %{x} <br><extra></extra>""",
89+
color="green",
90+
size=10
91+
)
92+
93+
),
94+
go.Scatter(
95+
x=data["2002"],
96+
y=countries,
97+
mode="markers",
98+
name="2002",
99+
marker=dict(
100+
color="blue",
101+
size=10
102+
)
91103
),
92104
]
93105
)
94106

95107
fig.update_layout(
96108
title="Life Expectancy in Europe: 1952 and 2002",
97109
height=1000,
98-
showlegend=False,
110+
legend_itemclick=False
99111
)
100112

101113
fig.show()
@@ -123,49 +135,64 @@ countries = (
123135
.sort_values(by=["lifeExp"], ascending=True)["country"]
124136
.unique()
125137
)
126-
127-
data = {"x": [], "y": [], "colors": [], "years": []}
138+
139+
data = {"line_x": [], "line_y": [], "1952": [], "2002": [], "colors": [], "years": [], "countries": []}
128140

129141
for country in countries:
130-
data["x"].extend(
142+
data["1952"].extend([df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0]])
143+
data["2002"].extend([df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0]])
144+
data["line_x"].extend(
131145
[
132146
df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0],
133147
df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0],
134148
None,
135149
]
136150
)
137-
data["y"].extend([country, country, None]),
138-
data["colors"].extend(["silver", "lightskyblue", "white"]),
139-
data["years"].extend(["1952", "2002", None])
151+
data["line_y"].extend([country, country, None]),
152+
140153

141154
fig = go.Figure(
142155
data=[
143156
go.Scatter(
144-
x=data["x"],
145-
y=data["y"],
157+
x=data["line_x"],
158+
y=data["line_y"],
146159
mode="markers+lines",
160+
showlegend=False,
147161
marker=dict(
148-
symbol="arrow", color="black", size=16, angleref="previous", standoff=8
149-
),
162+
symbol="arrow",
163+
color="black",
164+
size=16,
165+
angleref="previous",
166+
standoff=8
167+
)
168+
),
169+
go.Scatter(
170+
x=data["1952"],
171+
y=countries,
172+
name="1952",
173+
mode="markers",
174+
marker=dict(
175+
color="silver",
176+
size=16,
177+
)
150178
),
151179
go.Scatter(
152-
x=data["x"],
153-
y=data["y"],
154-
text=data["years"],
180+
x=data["2002"],
181+
y=countries,
182+
name="2002",
155183
mode="markers",
156184
marker=dict(
157-
color=data["colors"],
185+
color="lightskyblue",
158186
size=16,
159187
),
160-
hovertemplate="""Country: %{y} <br> Life Expectancy: %{x} <br> Year: %{text} <br><extra></extra>""",
161188
),
162189
]
163190
)
164191

165192
fig.update_layout(
166193
title="Life Expectancy in Europe: 1952 and 2002",
167194
height=1000,
168-
showlegend=False,
195+
legend_itemclick=False
169196
)
170197

171198

0 commit comments

Comments
 (0)