Python 如何使用plotly在X轴上以相同的方式缩放和平移所有子图
我有一个带有子图的绘图,子图是这样绘制的:Python 如何使用plotly在X轴上以相同的方式缩放和平移所有子图,python,plotly,Python,Plotly,我有一个带有子图的绘图,子图是这样绘制的: fig = make_subplots( rows=4, cols=1, subplot_titles=("Price, orders and positions", "Margin use", "PnL and fees", "Volume traded"), row_heights=[0.5, 0.2, 0.2, 0.1], ve
fig = make_subplots(
rows=4,
cols=1,
subplot_titles=("Price, orders and positions", "Margin use", "PnL and fees", "Volume traded"),
row_heights=[0.5, 0.2, 0.2, 0.1],
vertical_spacing=0.1
)
# price, orders, etc
fig.add_traces(
[
# draw price, average price and min / max
go.Scatter(name='Price', x=df.index, y=df['price'], mode='lines', line=dict(color='rgba(31, 119, 180, 1.)')),
go.Scatter(name='Average Price', x=df.index, y=df['average_price'], mode='lines', line=dict(color='rgba(31, 119, 180, 0.5)')),
go.Scatter(x=df.index, y=df['price_max'], mode='lines', marker=dict(color="#444"), line=dict(width=0), showlegend=False),
go.Scatter(x=df.index, y=df['price_min'], marker=dict(color="#444"), line=dict(width=0), mode='lines', fillcolor='rgba(68, 68, 68, 0.3)', fill='tonexty', showlegend=False),
# draw the long / short orders
go.Scatter(name='Long Open Price', x=df.index, y=df['orderlo_price'], mode='lines', line=dict(color='rgb(180, 119, 31)')),
go.Scatter(name='Long Close Price', x=df.index, y=df['orderlc_price'], mode='lines', line=dict(width=2, color='rgb(220, 159, 31)')),
go.Scatter(name='Short Open Price', x=df.index, y=df['orderso_price'], mode='lines', line=dict(color='rgb(119, 180, 31)')),
go.Scatter(name='Short Close Price', x=df.index, y=df['ordersc_price'], mode='lines', line=dict(width=2, color='rgb(159, 220, 31)')),
# add the position
go.Scatter(name='position', x=df.index, y=df['position_price'], mode='lines', line=dict(color='rgb(240, 200, 40)'))
],
rows=[1, 1, 1, 1, 1, 1, 1, 1, 1],
cols=[1, 1, 1, 1, 1, 1, 1, 1, 1]
)
# margin use
fig.add_traces(
[
go.Scatter(name='Margin use', x=df.index, y=df['margin_use'], mode='lines', line=dict(color='rgba(31, 119, 180, 1.)')),
#go.Scatter(name='Margin max', x=df.index, y=df['margin_max'], mode='lines', line=dict(color='rgba(31, 180, 119, 1.)'))
],
rows=[2], #, 2],
cols=[1], #, 1]
)
# PnL and fees
fig.add_traces(
[
go.Scatter(name='Unrealized PnL', x=df.index, y=df['unrealized_pnl'], line=dict(color='rgb(64, 255, 64, 1.0)')),
go.Scatter(name='Realized PnL', x=df.index, y=df['realized_pnl'], fill='tozeroy', line=dict(color='rgb(32, 192, 32, 0.8)')),
go.Scatter(name='Fees Paid', x=df.index, y=df['fees_paid'], fill='tozeroy', line=dict(color='rgb(192, 32, 32, 0.4)'))
],
rows=[3, 3, 3],
cols=[1, 1, 1]
)
# Volume traded
fig.add_trace(
go.Scatter(name='Volume Traded', x=df.index, y=df['volume_traded'], fill='tozeroy', line=dict(color='rgb(32, 128, 192, 1.0)')),
row=4,
col=1
)
# remove the margins
fig.update(
layout=go.Layout(
margin=go.layout.Margin(l=0.5, r=0.5, b=0.5, t=50)
)
)
如何确保所有子批次在X轴缩放和平移方面同步?现在,您可以放大一个子图,突然,该子图中的图形与其他子图没有关系。查看Plotly文档的部分。我相信这就是你要找的
本质上,将
shared_xaxes=True
添加到make_subplot()
函数中。因此看起来有两种方法可以实现这一点:shared_xaxes=True或fig.update_xaxes(matches='x');有什么区别?TBH,我不确定。也许,它们是相同底层功能的平等访问器?Plotly大量使用这种类型的逻辑。就我个人而言,我使用回答中提到的shared_xaxes
方法,因为它是明确的,并且在顶层实现,因此更易于阅读和维护。这两种方法都有效,但我更喜欢你建议的方法,因为正如你所说,它与其他设置处于顶层。