您可以将所有变量组合构建为轨迹和菜单。
cols_dd = ["Total tests", "Total cases", "Total deaths"]
hd = {
"Iso code": False,
"Vaccines": True,
"Total tests": ": ,0.f",
"Recent cases": ": ,0.f",
"Total cases": ": ,0.f",
"Total deaths": ": ,0.f",
"Total vaccinations": ": ,0.f",
"People vaccinated": ": ,0.f",
"Population": ": ,0.f",
"Vaccination policy": ": 0.f",
}
# px.scatter(df, x="Total cases", y="Total deaths", hover_data=hd, hover_name="Location")
fig = go.Figure()
for k, v in itertools.combinations(cols_dd, 2):
figt = px.scatter(df, x=k, y=v, hover_data=hd, hover_name="Location").update_traces(
visible=False
)
fig = fig.add_traces(figt.data)
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": f"{k} - {v}",
"method": "update",
"args": [
{
"visible": [
(k2 == k and v2 == v)
for k2, v2 in itertools.combinations(cols_dd, 2)
]
},
{"title": f"<b>{k} - {v}</b>"},
],
}
for k, v in itertools.combinations(cols_dd, 2)
]
}
],
margin={"l": 0, "r": 0, "t": 25, "b": 0},
).update_traces(visible=True, selector=0)
独立的
cols_dd = ["Total tests", "Total cases", "Total deaths"]
fig = go.Figure(
go.Scatter(
x=df[np.random.choice(cols_dd, 1)[0]],
y=df[np.random.choice(cols_dd, 1)[0]],
hovertemplate='x: %{x} <br>y: %{y}',
mode="markers"
)
)
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": f"x - {x}",
"method": "update",
"args": [
{"x": [df[x]]},
{"xaxis": {"title": x}},
],
}
for x in cols_dd
]
},
{
"buttons": [
{
"label": f"y - {x}",
"method": "update",
"args": [{"y": [df[x]]}, {"yaxis": {"title": x}}],
}
for x in cols_dd
],
"y": 0.9,
},
],
margin={"l": 0, "r": 0, "t": 25, "b": 0},
)
fig