pythonplotly - 排列奇数个子图

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

玩具示例:

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

fig = make_subplots(rows=2, cols=3,
                    shared_yaxes=True,
                   horizontal_spacing=0.02)

fig.add_scatter(x=[1,2,3],y=[1,2,3], row=1, col=1)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=1, col=2)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=1, col=3)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=2, col=1)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=2, col=2)

fig.update_layout(showlegend=False)
fig.show()

我想将底行的两个子图推到右边,这样它们就放在中间了。像这样的东西:

有没有办法以编程方式做到这一点?我尝试过使用

specs
make_subplots
选项,但没有成功。

python plotly subplot
1个回答
0
投票

我已经根据参考中的示例设置了您的子图。在这种情况下,要使第二行的图形居中,请使用两列,每列在第一行中有五行。第二行使用两列作为第二行和第三行。 None,即不在图表中的,必须包含在列数中。

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

fig = make_subplots(
    rows=2, cols=6,
    specs=[
        [{'colspan': 2}, None, {'colspan': 2}, None, {'colspan': 2},None],
        [None, {'colspan': 2}, None, {'colspan': 2}, None, None]
          ],
    shared_yaxes=True,
    horizontal_spacing=0.02,
    print_grid=True)

fig.add_scatter(x=[1,2,3],y=[1,2,3], row=1, col=1)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=1, col=3)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=1, col=5)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=2, col=2)
fig.add_scatter(x=[1,2,3],y=[1,2,3], row=2, col=4)

fig.update_layout(showlegend=False)
fig.show()

This is the format of your plot grid:
[ (1,1) x,y             -      ]  [ (1,3) x2,y2           -      ]  [ (1,5) x3,y3           -      ]
    (empty)      [ (2,2) x4,y4           -      ]  [ (2,4) x5,y5           -      ]      (empty)    

© www.soinside.com 2019 - 2024. All rights reserved.