Plotly:如何将水平滚动条添加到 plotly 表达图形? [英] Plotly: How to add a horizontal scrollbar to a plotly express figure?

查看:73
本文介绍了Plotly:如何将水平滚动条添加到 plotly 表达图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习更多关于 plotly 和 pandas 的知识,并且有一个多变量时间序列,我希望使用 plotly.express 功能进行绘制和交互.我还希望我的绘图具有水平滚动条,以便初始绘图用于预先指定的初始时间间隔.这是我的示例,涉及三个时间序列以及 10 万个时间点:

I'm beginning to learn more about plotly and pandas and have a multivariate time series I wish to plot and interact with using plotly.express features. I also want my plot to a horizontal scrollbar so that the initial plot is for a pre-specified initial time interval. Here's my example involving three time series along with 100K time points:

import plotly.express as px
import numpy as np
import pandas as pd

np.random.seed(123)
e = np.random.randn(100000,3)  
df=pd.DataFrame(e, columns=['a','b','c'])

df['x'] = df.index
df_melt = pd.melt(df, id_vars="x", value_vars=df.columns[:-1])
fig=px.line(df_melt, x="x", y="value",color="variable")
fig.show()

(对于我的最终目的,时间序列会更大——在 90 万多个时间点中可能有 40 到 70 个时间序列.)

(For my ultimate purposes, the time series will be larger--likely 40 to 70 time series in 900K+ time points.)

这会创建一个图表,我可以使用 plotly.express 功能与之交互,例如缩放、平移、矩形选择等.

This creates a graph with which I can interact using plotly.express features like zooming, panning, rectangle selection, etc.

有什么方法可以增加它,使初始图仅显示前 500 个时间点,并且滚动条允许我调查随着时间的增加会发生什么?

Is there a way I can augment this so that the initial plot shows merely the first 500 time points and a scroll bar permits me to investigate what happens as time increases?

在空闲时使用 Mac OS 10.15.4 和 Python 3.7.我希望在 IDLE 中而不是在 Jupyter notebook 环境中创建它.

Using Mac OS 10.15.4 and Python 3.7 with IDLE. I wish to create this in IDLE and not in a Jupyter notebook environment.

推荐答案

最简单的方法是将以下内容添加到您的设置中:

The easiest way is to add the following to your setup:

fig.update_layout(xaxis=dict(rangeslider=dict(visible=True),
                             type="linear"))

你会得到:

这将使您能够对原始图形进行子集化和平移:

This will enable you to both subset and pan the original figure:

完整代码:

import plotly.express as px
import numpy as np
import pandas as pd

np.random.seed(123)
e = np.random.randn(100000,3)  
df=pd.DataFrame(e, columns=['a','b','c'])

df['x'] = df.index
df_melt = pd.melt(df, id_vars="x", value_vars=df.columns[:-1])
fig=px.line(df_melt, x="x", y="value",color="variable")

# Add range slider
fig.update_layout(xaxis=dict(rangeslider=dict(visible=True),
                             type="linear")
)

fig.show()

这篇关于Plotly:如何将水平滚动条添加到 plotly 表达图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆