版面更新菜单:打开页面时按钮未激活(尽管设置了正确的选项)

时间:2018-12-17 13:32:50

标签: python widget plotly dropdown

我正在使用下面的代码使用户能够在四个不同的直方图中进行选择,以便基本上查看,即使设置了active = -1,showactive = false,该图仍会向我显示最初重叠的4个直方图。然后,当我单击下拉按钮时,它们就起作用了。如何避免出现最初的重叠问题,例如显示空图还是仅显示一个直方图?

谢谢。

import plotly
import plotly.graph_objs as go 
from datetime import datetime
import pandas_datareader as web
import pandas as pd
import numpy as np
df = pd.read_csv('... .csv')
date1 = go.Histogram(x=df['2018_09_13'],
                   name='Histogram of Seat Occupancy 2018/09/13', 
                   xbins=dict(start=0,size= 0.04),
                   autobinx = False, marker=dict(color='blue'))


date2 = go.Histogram(x=df['2018_09_14'],
                   name='Histogram of Seat Occupancy 2018/09/14', 
                   xbins=dict(start=0,size= 0.04),
                   autobinx = False, marker=dict(color='blue'))

date3 = go.Histogram(x=df['2018_09_15'],
                   name='Histogram of Seat Occupancy 2018/09/15', 
                   xbins=dict(start=0,size= 0.04),
                   autobinx = False, marker=dict(color='blue'))

date4 = go.Histogram(x=df['2018_09_16'],
                   name='Histogram of Seat Occupancy 2018/09/16',
                   xbins=dict(start=0,size= 0.04),
                   autobinx = False, marker=dict(color='blue'))

data = [date1, date2, date3, date4]


updatemenus = list([dict(active=-1, showactive=False, buttons=list([dict(label = '2018/09/13', method = 'update',args = [{'visible': [True, False, False, False]},
                                                           {'title': 'Histogram of Seat Occupancy 2018/09/13'}]),
                                              dict(label = '2018/09/14', method = 'update',
                                                   args = [{'visible': [False, True, False, False]},
                                                           {'title': 'Histogram of Seat Occupancy 2018/09/14'}]),
                                              dict(label = '2018/09/15', method = 'update', 
                                                   args = [{'visible': [False, False, True, False]},
                                                           {'title': 'Histogram of Seat Occupancy 2018/09/15'}]),
                                              dict(label = '2018/09/16', method = 'update', 
                                                   args = [{'visible': [False, False, False, True]},
                                                           {'title': 'Histogram of Seat Occupancy 2018/09/16'}])]))])

layout = go.Layout(autosize=False,
               width=1000,
               height=800,
               margin=go.layout.Margin(l=50,
                                       r=50,
                                       b=100,
                                       t=100,
                                       pad=4),
               paper_bgcolor='white',
               plot_bgcolor='#c7c7c7',
               title='Histogram of Seat Occupancy',
               showlegend=False,
               updatemenus=updatemenus,
               xaxis=dict(title='Occupancy (%)'),
               yaxis=dict(title='Count'),
               bargap=0.2,
               bargroupgap=0.1)

fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, auto_open=True, show_link=False)

0 个答案:

没有答案