[英]Plotly Express scatter plot: dropdown not working
I'm trying to add a dropdown box to a scatterplot built with plotly.express.我正在尝试将下拉框添加到使用 plotly.express 构建的散点图中。 So far, I've been able to add a dropdown box with the correct options but when I select one of them the graph doesn't update.
到目前为止,我已经能够添加一个带有正确选项的下拉框,但是当我输入 select 其中之一时,图表不会更新。 It is meant to filter between three options.
它旨在在三个选项之间进行过滤。 Is anyone able to advise me on what I'm doing wrong?
有谁能告诉我我做错了什么?
Code:代码:
fig = px.scatter(phl_crime, x="Age of accused", y = "Victim age", color="Charge", title="Relationship between victim and assailant age, Philadelphia homicides (x-y)", labels={"Age of accused": "Assailant age"})
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=["Charge", "Murder"],
label="Murder",
method="restyle"
),
dict(
args=["Charge", "Manslaughter"],
label="Manslaughter",
method="restyle"
),
dict(
args=["Charge", "Abortion"],
label="Abortion",
method="restyle"
)
]),
direction="down",
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.show()
Here is an image of the output这是 output 的图像
- the dropdown box is currently on the top left.
- 下拉框目前位于左上角。
UPDATE: Here is a sample of the phl_crime dictionary using phl_crime.head().to_dict()
:更新:这是使用
phl_crime.head().to_dict()
的 phl_crime 字典示例:
{'Year': {0: 1902, 1: 1902, 2: 1902, 3: 1902, 4: 1902}, 'Charge': {0: 'Murder', 1: 'Murder', 2: 'Murder', 3: 'Abortion', 4: 'Murder'}, 'Gender of accused': {0: 'Male', 1: 'Female', 2: 'Male', 3: 'Female', 4: 'Male'}, 'Age of accused': {0: nan, 1: nan, 2: nan, 3: 47.0, 4: nan}, 'Victim age': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan}, 'Weapon': {0: 'Fist, other body part', 1: nan, 2: 'Knife, sharp instrument', 3: nan, 4: 'Knife, sharp instrument'}, 'Gang': {0: 'Teen gang', 1: 'No gang', 2: 'No gang', 3: 'No gang', 4: 'No gang'}}
In case helpful to anyone else, I have now solved this by:如果对其他人有帮助,我现在已经通过以下方式解决了这个问题:
update
rather than restyle
methodupdate
而不是restyle
方法active
key to the updatemenus
dictactive
键添加到updatemenus
字典visible
key to the args
list & setting the values to True/False
(depending on whether the data for that label should appear or not).visible
键添加到args
列表并将值设置为True/False
(取决于 label 的数据是否应该出现)。 This is the revised, working code:这是修改后的工作代码:
fig = px.scatter(phl_crime, x="Age of accused", y = "Victim age", color="Charge", title="Relationship between victim and assailant age, Philadelphia homicides (x-y)", labels={"Age of accused": "Assailant age"})
fig.update_layout(title="Relationship between victim age and assailant age, Philadelphia homicides (x-y)",
xaxis=dict(
title="Age of accused"
),
yaxis=dict(
title="Age of victim"
))
# Add dropdown with dynamic titles
fig.update_layout(
updatemenus=[
dict(
active=0,
buttons=list([
dict(label="All charges",
method="update",
args=[{"visible": [True, True, True]},
{"title": "Relationship between victim and assailant age, Philadelphia homicides (x-y)",
"xaxis": {'title': "Age of accused"},
"yaxis": {'title': "Victim age"}}]),
dict(label="Murder",
method="update",
args=[{"visible": [True, False, False]},
{"title": "A dynamic title: ages of accused and victims in murder charges",
"xaxis": {'title': "Dynamic label: age of accused"},
"yaxis": {'title': "Dynamic label: victim age"}}]),
dict(label="Manslaughter",
method="update",
args=[{"visible": [False, False, True]},
{"title": "Another dynamic title: ages of accused and victims in manslaughter charges",
"xaxis": {'title': "Another dynamic label: age of accused"},
"yaxis": {'title': "Another dynamic label: victim age"}}]),
dict(label="Abortion",
method="update",
args=[{"visible": [False, True, False]},
{"title": "More dynamism: ages of accused and victims in abortion charges",
"xaxis": {'title': "More dynamism: age of accused"},
"yaxis": {'title': "More dynamism: victim age"}}])
]),
)
])
(Please ignore the added titles/x-axis/y-axis data as not relevant). (请忽略不相关的添加标题/x 轴/y 轴数据)。 The dropdown box is now working.
下拉框现在可以使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.