【问题标题】:How to limit update_layout margins only to one subplot in Plotly / Dash?如何将 update_layout 边距限制为 Plotly / Dash 中的一个子图?
【发布时间】:2021-11-17 05:21:17
【问题描述】:

在 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)

填充应用于mainsub

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

【问题讨论】:

    标签: python python-3.x plotly plotly-dash plotly-python


    【解决方案1】:

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

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

    完整代码:

    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()
    

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2018-01-16
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多