[英]How to create a bar plot with shared x-axis using plotly.express with sub-titles on top?
[英]Plotly: How to plot multiple lines with shared x-axis?
我想在同一個 canvas 內有一條多行 plot 與同一個 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()
我可以知道如何做到這一點,如果有人能指出好的閱讀材料,不勝感激
使用這樣的數據集,您可以 select 任意數量的列,使用fig = make_subplots()
設置一個圖形, shared_xaxes
設置為True
,然后使用fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1)
在一個循環中得到這個:
讓我知道這是否是您可以使用但需要稍作調整的設置。
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()
根據您繪制的數據,我認為您可以查看https://plotly.com/python/subplots/上的“具有共享 X 軸(低級 API)的堆疊子圖”
或者通過向上移動每行 plot 來分離數據,如下所示:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.