显示/隐藏Plotly中的按钮,Python

时间:2018-07-30 15:39:45

标签: python plotly

我有以下图表,其中包含2个更新按钮。

enter image description here

我想修改以下代码,以便当我按下一个按钮时,相应的图将出现,而当我“取消”它时,它将消失。 我该如何实现?

代码:

import numpy as np
from scipy import stats

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)

RV = np.random.normal(size=1000)
r = max(max(RV),-min(RV))
xrange = np.arange(-1.2*r,1.2*r,0.0025)
hist = go.Histogram(x=RV, 
                    opacity=0.75, 
                    histnorm='probability density',
                    showlegend=False
                    )

pdf = go.Scatter(x=xrange, 
                 y=stats.norm(loc=np.mean(RV), scale=np.std(RV)).pdf(xrange),
                 mode='lines',              
                 line=dict(width=1.5, 
                           shape='spline'),
                 visible=False,
                 showlegend=False
                 )

data = [hist, pdf]

updatemenus = list([
    dict(type="buttons",
         active=-1,
         buttons=list([
            dict(label = 'Histogram',
                 method = 'update',
                 args = [{'visible': [True, False]}]),
            dict(label = 'Normal PDF',
                 method = 'update',
                 args = [{'visible': [True, True]}]),
        ]),
    )
])

layout = dict(showlegend=False,
              xaxis = dict(range = [-r,r]),
              updatemenus=updatemenus)

fig = dict(data=data, layout=layout)
iplot(fig, show_link=False)

0 个答案:

没有答案