繁体   English   中英

Plotly - 在 createtd 跟踪后更新子图标题

[英]Plotly - update subplot titles after traces where createtd

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

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或类似的东西来做吗?

当使用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()

在此处输入图片说明

您可以按如下方式遍历 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

尝试这个,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM