绘图下拉输出图表

2024-09-27 00:13:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎弄不懂文件。基本上我想创建一个下拉菜单,每个选择输出一个不同的图表。这是一个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')

基本上,我想创建一个下拉菜单[‘种族’、‘教育’、‘收入’],输出相应的图表


Tags: csvhttpsgithubcomreadpdhaspercent
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:08

我已将您的数据制作成一个图形,您可以在“plotly”中的“go”下拉列表中选择该图形。默认设置是显示所有值。我在官方{a1}和{a2}的帮助下修改了它

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

enter image description here

enter image description here

相关问题 更多 >

    热门问题