下面是我的代码
# imports
import plotly.graph_objs as go
import pandas as pd
# plotly figure setup
fig = go.Figure()
# one trace for each df column
for brand in final_df2.brand.unique():
df = final_df2[final_df2.brand == brand]
fig.add_trace(go.Scatter(x=df.date, y=[df["price"].values,df["SHB Car price"].values],
name = brand,
mode = 'lines')
)
# one button for each df column
updatemenu= []
buttons=[]
for brand in final_df2.brand.unique():
df = final_df2[final_df2.brand == brand]
buttons.append(dict(method='restyle',
label=brand,
args=[{'y':[df["price"].values, df["SHB Car price"].values]}])
)
# some adjustments to the updatemenu
updatemenu=[]
your_menu=dict()
updatemenu.append(your_menu)
updatemenu[0]['buttons']=buttons
updatemenu[0]['direction']='down'
updatemenu[0]['showactive']=True
# update layout and show figure
fig.update_layout(updatemenus=updatemenu)
fig.show()
每个品牌应有一个按钮,按下该按钮时,应显示SHB汽车价格和价格。但是,使用上述代码将所有图形聚集在一个图中,如下所示:
这就是数据帧的外观:
{'date': {11: Timestamp('2020-04-21 00:00:00'),
12: Timestamp('2020-04-22 00:00:00'),
14: Timestamp('2020-04-24 00:00:00'),
17: Timestamp('2020-04-24 00:00:00'),
19: Timestamp('2020-04-24 00:00:00'),
22: Timestamp('2020-04-26 00:00:00'),
23: Timestamp('2020-04-26 00:00:00'),
25: Timestamp('2020-04-26 00:00:00'),
27: Timestamp('2020-04-27 00:00:00'),
28: Timestamp('2020-04-27 00:00:00')},
'SHB Car price': {11: 65950.0,
12: 80000.0,
14: 81500.0,
17: 104500.0,
19: 155000.0,
22: 88500.0,
23: 89750.0,
25: 99000.0,
27: 65900.0,
28: 107000.0},
'price': {11: nan,
12: nan,
14: nan,
17: nan,
19: nan,
22: nan,
23: nan,
25: nan,
27: nan,
28: nan},
'brand': {11: 'opel',
12: 'opel',
14: 'opel',
17: 'toyota',
19: 'volvo',
22: 'kia',
23: 'nissan',
25: 'toyota',
27: 'dacia',
28: 'kia'}}
此数据框在“品牌”列中包含不同的品牌。对于每个品牌,我需要绘制SHB汽车价格和价格
目前没有回答
相关问题 更多 >
编程相关推荐