使用plotly从多个csv文件打印一个图形

2024-09-30 20:16:46 发布

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

我想使用plotly创建一个交互式图表,并包括一个下拉菜单,涵盖每周、每月、季度和年度商品价格指数。如何从这四个单独的csv文件中提取数据

dfq=pd.read_csv('Quaterly_BoC Commodity Index.csv')
enter code here`dfm=pd.read_csv('Monthly_BoC Commodity Index.csv')
enter code here`dfy=pd.read_csv('Annual_BoC Commodity Index.csv')


dfw=pd.read_csv('Weekly BoC Commodity Index.csv')

## I started with weekly file
trace_BCPI=go.Scatter( x=list(dfw.date ),
                      y=list(dfw.BCPI), 
                      name='BCPI',  line=dict(color='#33CFA5'))

trace_BCPI_avg= go.Scatter (x=list(dfw.date),
                            y=[dfw.BCPI.mean()]*len(dfw.date),
                            name='BCPI average',
                            visible= False)
BCPI_annotations=[dict(x='2016-03-01',
                       y=dfw.BCPI.mean(),
                       xref='x', yref='y',
                       text='BCPI Average:<br>'+str(dfw.BCPI.mean())),
                  dict(x=dfw.BCPI.idxmax(),
                       y=dfw.BCPI.max(),
                       xref='x', yref='y',
                       text='BCPI Max:<br>'+str(dfw.BCPI.max())),
                 dict(x=dfw.BCPI.idxmin(),
                       y=dfw.BCPI.min(),
                       xref='x', yref='y',
                       text='BCPI Min:<br>'+str(dfw.BCPI.min()),
                    )]



layout=dict(title='Bank of Canada commodity price index', showlegend= False)
fig = dict(data=data, layout=layout)

iplot(fig)

## now I want to add a dropdown menu to include stats from monthly, quarterly and annual csv file. Any suggestion? thank you!

Tags: csvtextreaddateindexmeandictlist