【问题标题】:Plotly - update subplot titles after traces where createtdPlotly - 在跟踪 createtd 后更新子图标题
【发布时间】:2020-11-02 12:09:13
【问题描述】:

我有一个由子图组成的情节 -

fig = make_subplots(
        rows=3, cols=1)

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

我要为每个子图添加一个标题,但只有在创建了图形并添加了跟踪之后。即不是当我创建图形时-

fig = make_subplots(
            rows=3, cols=1, subplot_titles=['a', 'b', 'c']

我可以通过fig.update_layout 或类似的方式进行吗?

【问题讨论】:

标签: python plotly


【解决方案1】:

当使用make_subplots 时,它会创建(正确放置的)注释。因此,这可以大部分使用注释方法进行复制。一般来说,对于第一个:

fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="a", row=1, col=1)

缺点是您可能需要根据自己的条件/口味调整 xy

完整示例,使用粗体:

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

fig = make_subplots(rows=3, cols=1)
    
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="<b>Hey</b>", row=1, col=1)

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=2, col=1)
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="<b>Bee</b>", row=2, col=1)

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=3, col=1)
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
                   text="<b>See</b>", row=3, col=1)

fig.show()

【讨论】:

    【解决方案2】:

    您可以按如下方式遍历 xaxes。这会在每个子图上设置 x 轴标题:

    for i in range(3):
        fig['layout'][f'xaxis{i+1}'].update(title=f'Title {i+1}')
    

    各轴相关问题设置showgridPlotly set showgrid = False for ALL subplots

    【讨论】:

      【解决方案3】:

      试试这个,

      fig.update_layout(margin=dict(l=20, r=20, t=20, b=20), paper_bgcolor="LightSteelBlue", xaxis_title='X Label Name', yaxis_title="Y Label Name")
      

      【讨论】:

      • 谢谢,我想更新每个子图的标题,而不是轴标题。
      猜你喜欢
      • 2022-06-11
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2023-03-31
      • 1970-01-01
      • 2021-07-30
      相关资源
      最近更新 更多