Plotly:如何绘制具有共享 x 轴的多条线?
Plotly: How to plot multiple lines with shared x-axis?
我想在同一个 canvas 内绘制一个多线图,并与 figure 中显示的相同 x 轴相关联:
使用子图未达到预期目的。
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
根据您绘制的数据,我认为您可以查看 https://plotly.com/python/subplots/ 上的“具有共享 X-Axis (low-level API) 的堆叠子图”
或者像这样向上移动每条线图来分离数据:
import plotly.graph_objects as go
import random
data = []
n = 9
for x in range(10, 60, 10):
points = [value + x for value in random.sample(range(1,n+1), k = n)]
data.append(go.Scatter(y=points))
fig = go.Figure(data = data)
fig.show()
使用 this 等数据集,您可以 select 任意数量的列,使用 fig = make_subplots()
设置图形,将 shared_xaxes
设置为 True
然后在循环中使用 fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1)
使用共享 x-axis 添加您的系列以获取此内容:
让我知道这是否是您可以使用但需要稍作调整的设置。
完整代码:
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()
我想在同一个 canvas 内绘制一个多线图,并与 figure 中显示的相同 x 轴相关联:
使用子图未达到预期目的。
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
根据您绘制的数据,我认为您可以查看 https://plotly.com/python/subplots/ 上的“具有共享 X-Axis (low-level API) 的堆叠子图”
或者像这样向上移动每条线图来分离数据:
import plotly.graph_objects as go
import random
data = []
n = 9
for x in range(10, 60, 10):
points = [value + x for value in random.sample(range(1,n+1), k = n)]
data.append(go.Scatter(y=points))
fig = go.Figure(data = data)
fig.show()
使用 this 等数据集,您可以 select 任意数量的列,使用 fig = make_subplots()
设置图形,将 shared_xaxes
设置为 True
然后在循环中使用 fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1)
使用共享 x-axis 添加您的系列以获取此内容:
让我知道这是否是您可以使用但需要稍作调整的设置。
完整代码:
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()