您的位置:首页 > 游戏 > 手游 > Plotly库利用滑块创建数据可视化

Plotly库利用滑块创建数据可视化

2024/10/7 0:27:15 来源:https://blog.csdn.net/weixin_50951788/article/details/139087987  浏览:    关键词:Plotly库利用滑块创建数据可视化

使用了Plotly库来创建一个数据可视化图表,并使用滑块来控制显示哪些数据

import plotly.graph_objects as go
from plotly.subplots import make_subplots# 示例数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [5, 4, 3, 2, 1]
y3 = [2, 3, 1, 5, 4]# 创建子图
fig = make_subplots(rows=1, cols=1)# 添加初始数据
fig.add_trace(go.Scatter(x=x, y=y1, name="Data 1", visible=True),row=1, col=1
)
fig.add_trace(go.Scatter(x=x, y=y2, name="Data 2", visible=False),row=1, col=1
)
fig.add_trace(go.Scatter(x=x, y=y3, name="Data 3", visible=False),row=1, col=1
)# 创建滑块
steps = []
for i, y in enumerate([y1, y2, y3]):step = dict(method="update",args=[{"visible": [False] * 3}, {"title": "Data {}".format(i+1)}],label="Data {}".format(i+1))step["args"][0]["visible"] = [False] * 3step["args"][0]["visible"][i] = Truesteps.append(step)sliders = [dict(active=0,currentvalue={"prefix": "Show: "},steps=steps
)]# 更新布局
fig.update_layout(sliders=sliders,title="Data Visualization"
)# 显示图表
fig.show()

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com