Plotly 下拉输出图表
Plotly Dropdown output chart
我似乎无法理解文档。基本上我想创建一个下拉菜单,每个选择输出一个不同的图表。这是一个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'] 的下拉菜单来输出相应的图表。
我已将您的数据制作成图表,您可以在 drop-down 中 select 和 'plotly' 中的 'go'。默认是显示所有值。我在官方的帮助下修改了reference and this 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()
我似乎无法理解文档。基本上我想创建一个下拉菜单,每个选择输出一个不同的图表。这是一个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'] 的下拉菜单来输出相应的图表。
我已将您的数据制作成图表,您可以在 drop-down 中 select 和 'plotly' 中的 'go'。默认是显示所有值。我在官方的帮助下修改了reference and this 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()