使用gapminder数据通过下拉菜单创建绘图线图

时间:2020-07-10 01:43:49

标签: python plot graph plotly

我正在尝试使用以下代码创建一个下拉菜单:

import plotly.express as px

df = px.data.gapminder()
fig = px.line(df, x="year", y="lifeExp", color="country")


buttons = [
    {'method' : 'update', 'label' : val, 'args' : df[df['continent'].eq(val)]['lifeExp']} 
    for val in df['continent'].unique()
]

# construct menus
updatemenus = [{'buttons': buttons,
                'direction': 'down',
                'showactive': True,}]

# update layout with buttons, and show the figure
fig.update_layout(updatemenus=updatemenus)
fig.show()

我已经添加了列表推导来生成y值,但是我不确定在这里我当前做错了什么。

我希望能够选择其他大陆并进行情节更新

0 个答案:

没有答案