I'm trying to add a dropdown box to a scatterplot built with 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. 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
- 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():
{'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'}}
phl_crime.head().to_dict()into your question – this will allow people to reproduce your dropdown error – thanks!