簡體   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