【问题标题】:Plotly Dropdown output chartPlotly Dropdown 输出图表
【发布时间】:2020-10-20 22:27:54
【问题描述】:

我似乎无法弄清楚文档。基本上我想创建一个下拉菜单,每个选择输出一个不同的图表。这是一个MRE。 plotly.express 导入为 px。

race = pd.read_csv('https://github.com/ngpsu22/Ed_Debt-vs.-UBI/raw/main/race_debt_ubi')
education = pd.read_csv('https://github.com/ngpsu22/Ed_Debt-vs.-UBI/raw/main/education_debt_ubi')
income = pd.read_csv('https://github.com/ngpsu22/Ed_Debt-vs.-UBI/raw/main/income_debt_ubi')

fig_race = px.bar(race, x='race', y='percent_has_debt', text='percent_has_debt')
fig_education = px.bar(education,  y='percent_has_debt', text='percent_has_debt')
fig_income = px.bar(income,  y='percent_has_debt', text='percent_has_debt')

基本上我想创建一个 ['race', 'education', 'income'] 的下拉菜单来输出相应的图表。

【问题讨论】:

    标签: python plotly dropdown


    【解决方案1】:

    我已将您的数据制作成图表,您可以使用“plotly”中的“go”在下拉菜单中进行选择。默认是显示所有值。我在官方referencethis site的帮助下修改了。

    import plotly.graph_objects as go
    import pandas as pd
    
    race = pd.read_csv('https://github.com/ngpsu22/Ed_Debt-vs.-UBI/raw/main/race_debt_ubi')
    education = pd.read_csv('https://github.com/ngpsu22/Ed_Debt-vs.-UBI/raw/main/education_debt_ubi')
    income = pd.read_csv('https://github.com/ngpsu22/Ed_Debt-vs.-UBI/raw/main/income_debt_ubi')
    
    fig = go.Figure()
    
    fig.add_trace(go.Bar(x=race['race'], y=race['percent_has_debt'], name='race'))
    fig.add_trace(go.Bar(x=[0,1,2,3], y=education['percent_has_debt'], name='education'))
    fig.add_trace(go.Bar(x=[0,1,2,3,4,5], y=income['percent_has_debt'], name='income'))
    
    fig.update_layout(
        updatemenus=[go.layout.Updatemenu(
            active=0,
            buttons=list([
                dict(label="None",
                     method="update",
                     args=[{'visible':[False,False,False]},
                           {'title':'AL','showlegend':True}]),
                dict(label="race",
                     method="update",
                     args=[{'visible':[True,False,False]},
                           {'title':'RACE','showlegend':True}]),
                dict(label="education",
                     method="update",
                     args=[{'visible':[False,True,False]},
                           {'title':'EDUCATION','showlegend':True}]),
                dict(label="income",
                     method="update",
                     args=[{'visible':[False,False,True]},
                           {'title':'INCOME','showlegend':True}]),
        ]
    ))])
    
    
    fig.show()
    

    【讨论】:

    • 这太棒了,正是我想要的。有没有办法让它在没有 none 选项的情况下工作?
    • 您可以从按钮列表中删除 'dict(label='None'...)`。
    • 当我尝试摆脱它时......默认跟踪仍然具有所有图表,直到我单击其中一个结果然后它们从那里正确更新
    • 我错了。无”应该是“可见的”:一切都应该是假的。我正在尝试弄清楚如何在加载时不显示任何内容。
    • 是的,我也想不通....感谢您的尝试
    猜你喜欢
    • 2021-09-14
    • 2020-02-19
    • 2020-02-15
    • 2017-12-31
    • 1970-01-01
    • 2022-01-16
    • 2022-01-28
    • 2016-08-13
    • 2018-06-13
    相关资源
    最近更新 更多