直接从plotly的tut页面基于此代码:
https://plot.ly/python/dropdowns/
现在,如果我不仅要更改图表类型而是更改数据源及其图表类型,该怎么办? 可能吗?
编辑:
我玩过这个设置:
data1 = go.Surface(z=df.values.tolist(), colorscale='Viridis')
data2 = go.Heatmap(z=df.values.tolist())
buttons=list([
dict(
args=[data1],
label='3D Surface',
method='restyle'
),
dict(
args=[data2],
label='Heatmap',
method='restyle'
)
])
但是,图表会显示,但会重叠。当我点击下拉菜单中的任何项目时,图表就完全消失了。
答案 0 :(得分:4)
我自己找到了一个解决方案,实际上是基于tut:
import plotly
import plotly.graph_objs as go
from datetime import datetime
import pandas_datareader as web
df = web.DataReader("aapl", 'google',
datetime(2015, 1, 1),
datetime(2016, 7, 1))
trace_high = go.Bar( x=df.index,
y=df.High,
name='High')
trace_low = go.Scatter(x=df.index,
y=df.Low,
name='Low',
line=dict(color='#F06A6A'))
data = [trace_high, trace_low]
updatemenus = list([
dict(active=-1,
buttons=list([
dict(label = 'High',
method = 'update',
args = [{'visible': [True, False]},
{'title': 'Yahoo High'}]),
dict(label = 'Low',
method = 'update',
args = [{'visible': [False, True]},
{'title': 'Yahoo Low'}])
]),
)
])
layout = dict(title='Yahoo', showlegend=False,
updatemenus=updatemenus)
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, auto_open=False, show_link=False)