Skip to content

Commit

Permalink
Merge pull request #3962 from plotly/dumbbell-updates-2
Browse files Browse the repository at this point in the history
Add legend to dumbbell-plots
  • Loading branch information
nicolaskruchten authored Jan 23, 2023
2 parents edb8de2 + 1da06f9 commit db6746e
Showing 1 changed file with 58 additions and 32 deletions.
90 changes: 58 additions & 32 deletions doc/python/dumbbell-plots.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,47 +55,59 @@ countries = (
.unique()
)

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

for country in countries:
data["x"].extend(
data["1952"].extend([df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0]])
data["2002"].extend([df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0]])
data["line_x"].extend(
[
df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0],
df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0],
None,
]
)
data["y"].extend([country, country, None]),
data["colors"].extend(["green", "blue", "brown"]),
data["years"].extend(["1952", "2002", None])
data["line_y"].extend([country, country, None]),

fig = go.Figure(
data=[
go.Scatter(
x=data["x"],
y=data["y"],
x=data["line_x"],
y=data["line_y"],
mode="lines",
showlegend=False,
marker=dict(
color="grey",
),
color="grey"
)
),
go.Scatter(
x=data["x"],
y=data["y"],
mode="markers+text",
x=data["1952"],
y=countries,
mode="markers",
name="1952",
marker=dict(
color=data["colors"],
size=10,
),
hovertemplate="""Country: %{y} <br> Life Expectancy: %{x} <br><extra></extra>""",
color="green",
size=10
)

),
go.Scatter(
x=data["2002"],
y=countries,
mode="markers",
name="2002",
marker=dict(
color="blue",
size=10
)
),
]
)

fig.update_layout(
title="Life Expectancy in Europe: 1952 and 2002",
height=1000,
showlegend=False,
legend_itemclick=False
)

fig.show()
Expand Down Expand Up @@ -124,48 +136,62 @@ countries = (
.unique()
)

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

for country in countries:
data["x"].extend(
data["1952"].extend([df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0]])
data["2002"].extend([df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0]])
data["line_x"].extend(
[
df.loc[(df.year == 1952) & (df.country == country)]["lifeExp"].values[0],
df.loc[(df.year == 2002) & (df.country == country)]["lifeExp"].values[0],
None,
]
)
data["y"].extend([country, country, None]),
data["colors"].extend(["silver", "lightskyblue", "white"]),
data["years"].extend(["1952", "2002", None])
data["line_y"].extend([country, country, None]),

fig = go.Figure(
data=[
go.Scatter(
x=data["x"],
y=data["y"],
x=data["line_x"],
y=data["line_y"],
mode="markers+lines",
showlegend=False,
marker=dict(
symbol="arrow", color="black", size=16, angleref="previous", standoff=8
),
symbol="arrow",
color="black",
size=16,
angleref="previous",
standoff=8
)
),
go.Scatter(
x=data["1952"],
y=countries,
name="1952",
mode="markers",
marker=dict(
color="silver",
size=16,
)
),
go.Scatter(
x=data["x"],
y=data["y"],
text=data["years"],
x=data["2002"],
y=countries,
name="2002",
mode="markers",
marker=dict(
color=data["colors"],
color="lightskyblue",
size=16,
),
hovertemplate="""Country: %{y} <br> Life Expectancy: %{x} <br> Year: %{text} <br><extra></extra>""",
),
]
)

fig.update_layout(
title="Life Expectancy in Europe: 1952 and 2002",
height=1000,
showlegend=False,
legend_itemclick=False
)


Expand Down

0 comments on commit db6746e

Please sign in to comment.