[英]Plotly - update subplot titles after traces where createtd
I have a plotly plot composed of 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_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)
I was to add a title for every the subplots but only after the figure is created and the traces are added.我要为每个子图添加一个标题,但只有在创建图形并添加跟踪之后。 Ie not when I create the figure -即不是当我创建图形时 -
fig = make_subplots(
rows=3, cols=1, subplot_titles=['a', 'b', 'c']
Can I do it via fig.update_layout
or something similar?我可以通过fig.update_layout
或类似的东西来做吗?
When make_subplots
is used it's creating (correctly placed) annotations.当使用make_subplots
,它会创建(正确放置)注释。 So this can be mostly duplicated using annotation methods.因此,这主要可以使用注释方法进行复制。 In general, for the first:一般来说,对于第一个:
fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False,
text="a", row=1, col=1)
The downside is you may need to adjust x
and y
to your conditions/tastes.缺点是您可能需要根据您的条件/口味调整x
和y
。
Full example, using bold text:完整示例,使用粗体文本:
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()
You can iterate through the xaxes as follows.您可以按如下方式遍历 xaxes。 This sets the x axis title on each subgraph:这在每个子图上设置 x 轴标题:
for i in range(3):
fig['layout'][f'xaxis{i+1}'].update(title=f'Title {i+1}')
Related question setting showgrid
on each axis: Plotly set showgrid = False for ALL subplots相关问题在每个轴上设置showgrid
: Plotly 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.