簡體   English   中英

Plotly Express scatter plot:下拉菜單不起作用

[英]Plotly Express scatter plot: dropdown not working

我正在嘗試將下拉框添加到使用 plotly.express 構建的散點圖中。 到目前為止,我已經能夠添加一個帶有正確選項的下拉框,但是當我輸入 select 其中之一時,圖表不會更新。 它旨在在三個選項之間進行過濾。 有誰能告訴我我做錯了什么?

代碼:

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()

這是 output 的圖像在此處輸入圖像描述 - 下拉框目前位於左上角。

更新:這是使用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'}}

如果對其他人有幫助,我現在已經通過以下方式解決了這個問題:

  1. 使用update而不是restyle方法
  2. active鍵添加到updatemenus字典
  3. visible鍵添加到args列表並將值設置為True/False (取決於 label 的數據是否應該出現)。

這是修改后的工作代碼:

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"}}])
            ]),
        )
    ])

(請忽略不相關的添加標題/x 軸/y 軸數據)。 下拉框現在可以使用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM