如何将 update_layout 边距限制为 Plotly / Dash 中的一个子图?

问题描述 投票:0回答:1

在 2 行 1 列布局中,

main
图位于
sub
上方。

    fig.add_trace(go.Scatter(x=df.index, y=main_data, name='main',
                             line=dict(color='white', width=1), row=1, col=1)

    fig.add_trace(go.Bar(x=df.index, y=sub_data, name='sub', row=2, col=1)

当我如下使用

update_layout
时:

    fig.update_layout(height=400, margin=dict(t=30, b=15, l=15), pad=20)

填充应用于两者

main
sub

有没有办法让填充仅适用于

main

python plotly plotly-dash plotly-python
1个回答
2
投票

fig.update_layout()
仅适用于整个图形的属性,这就是为什么您不能像使用
fig.update_layout(row = 2, col = 2)
那样使用
fig.update_traces(row, col)
来处理子图的属性。因此,根据您想要在这里实现的目标,您必须在 specsrow_heights
 调用中通过 
 和/或 
column_widths
make_subplots()
来调整子图的外观。

这是使用这两种方法的示例:

完整代码:

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

fig = make_subplots(
    rows=5, cols=2,
    column_widths = [0.7, 0.3],
    row_heights = [0.2, 0.2, 0.2, 0.1, 0.1],
    specs=[[{}, {"rowspan": 2}],
           [{}, None],
           [{"rowspan": 2, "colspan": 2}, None],
           [None, None],
           [{}, {}]],
#     print_grid=True
)

fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2)

fig.update_layout(height=600, width=600, title_text="specs examples")
fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.