How to change data and graph using buttons in plotly python?(如何使用 plotly python 中的按钮更改数据和图形?)
问题描述
基于此代码直接来自 plotly 的 tut 页面:
Based on this code directly from plotly's tut page:
https://plot.ly/python/dropdowns/
现在,如果我不仅要更改图表类型,还要更改数据源及其图表类型,该怎么办?是否可以?
Now, what if I want to change not just the chart type but rather the data source and its chart type? Is it possible?
我玩过这个设置:
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'
)
])
但是,图表显示了,但被覆盖了.当我点击下拉菜单中的任何项目时,图表完全消失了.
However, the graphs are shown, but overlayed. And when I click any item in the dropdown menu, the graph is completely gone.
推荐答案
我自己找到了一个解决方案,其实就是基于tut:
I've found a solution myself, that is actually based on the 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)
这篇关于如何使用 plotly python 中的按钮更改数据和图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 plotly python 中的按钮更改数据和图形?
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01