[英]Change subplots title position/orientation in Plotly Python
I need to change subplot title in python plotly, namely, rotate it by 90 degrees.我需要更改 python plotly 中的子图标题,即旋转 90 度。 I tried hard but without any success.
我努力尝试但没有成功。
Here is my code这是我的代码
import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools
trace1 = go.Bar(
x=[1, 2, 3],
y=[10, 11, 12]
)
trace2 = go.Bar(
x=[1, 2, 3],
y=[100, 110, 120],
)
trace3 = go.Bar(
x=[1, 2, 3],
y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=1, cols=3,
shared_xaxes=True, shared_yaxes=True,
vertical_spacing=0.001,
subplot_titles = ('first_title', 'second_title', 'third_title'))
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 1, 3)
fig['layout'].update(height=600, width=600, title='main_title')
pyo.plot(fig, filename='file.html')
So, I want to rotate 'first_title'
, 'second_title'
and 'third_title'
by 90 degrees so that they do not overlap at each other.所以,我想将
'first_title'
、 'second_title'
和'third_title'
旋转 90 度,这样它们就不会相互重叠。 Is it possible to tackle this problem?有可能解决这个问题吗?
you can simply add this code before the pyo.plot(fig, filename='file.html')
line:您可以简单地在
pyo.plot(fig, filename='file.html')
行之前添加此代码:
for annotation in fig['layout']['annotations']:
annotation['textangle']=-90
However, this will cause subplot titles overlapping the main title, so I suggest you to remove it.但是,这会导致子情节标题与主标题重叠,因此我建议您将其删除。 This is the final code:
这是最终的代码:
import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools
trace1 = go.Bar(
x=[1, 2, 3],
y=[10, 11, 12]
)
trace2 = go.Bar(
x=[1, 2, 3],
y=[100, 110, 120],
)
trace3 = go.Bar(
x=[1, 2, 3],
y=[1000, 1100, 1200],
)
fig = tools.make_subplots(rows=1, cols=3,
shared_xaxes=True, shared_yaxes=True,
vertical_spacing=0.001,
subplot_titles = ('first_title', 'second_title', 'third_title'))
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 1, 3)
fig['layout'].update(height=600, width=600, title='')
# rotate all the subtitles of 90 degrees
for annotation in fig['layout']['annotations']:
annotation['textangle']=-90
pyo.plot(fig, filename='file.html')
This answer is linked to Can you alter a subplot title location in plotly?这个答案链接到Can you alter a subplot title location in plotly? which seeks an answer to getting the subplot tiles aligned correctly.
它寻求正确对齐子图块的答案。
I had a similar problem with subplot titles on a 2 x 2 grid.我在 2 x 2 网格上遇到了类似的子图标题问题。 The answer was simply
答案很简单
fig.update_annotations(yshift=20)
You could also update text angles in one line您还可以在一行中更新文本角度
fig.update_annotations(textangle=-90)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.