python - Plotly:如何用共享的 x 轴绘制多条线?

标签 python plotly plotly-dash

我想在同一个 Canvas 内有一个多线图,并与相同的 x 轴绑定(bind),如 figure 中所示。 :
enter image description here
使用子图并不能达到预期的目的。

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=2, shared_xaxes=True,vertical_spacing=0.1)
fig.add_scatter(y=[2, 1, 3], row=1, col=1)
fig.add_scatter(y=[1, 3, 2], row=2, col=1)
fig.show()
我可以知道如何做到这一点,如果有人能指出好的阅读 Material ,不胜感激

最佳答案

使用 this 等数据集您可以选择任意数量的列,使用 fig = make_subplots() 设置图形与 shared_xaxes设置为 True然后使用 fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1) 添加具有共享 x 轴的系列在一个循环中得到这个:
enter image description here
让我知道这是否是您可以使用但需要稍作调整的设置。
完整代码:

import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots
import pandas as pd

# data
pio.templates.default = "plotly_white"
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df = df.set_index('Date')
df.tail()
cols = df.columns[:-4]
ncols = len(cols)

# subplot setup
fig = make_subplots(rows=ncols, cols=1, shared_xaxes=True)

for i, col in enumerate(cols, start=1):
    fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1)
    
fig.show()

关于python - Plotly:如何用共享的 x 轴绘制多条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63265707/

相关文章:

python - TFLearn - 什么是输入数据

python - 在 Dash 中构建仪表板

Python、__slots__、继承和类变量==>属性是只读的bug

python - 递归函数中数据库连接的最佳实践?

Python Dict() 根据日期时间排序

python - Plotly:如何为箱线图中的每个点分配唯一的名称?

python - 需要帮助了解如何使用plotly绘制3D曲面图

python - 斧头反转时无法设置范围-plotly

python - Plotly Dash Table 仅在一侧应用边框半径

plotly-dash - Plotly Dash - dcc.Store 回调触发两次