我需要在python中更改子图标题,即将其旋转90度。我很努力,但没有成功。
这是我的代码
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')
因此,我想将'first_title'
,'second_title'
和'third_title'
旋转90度,以使它们彼此不重叠。有可能解决这个问题吗?
答案 0 :(得分:2)
您只需在pyo.plot(fig, filename='file.html')
行之前添加以下代码:
for annotation in fig['layout']['annotations']:
annotation['textangle']=-90
但是,这将导致子图标题与主标题重叠,因此建议您删除它。这是最终代码:
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')
答案 1 :(得分:1)
此答案与 Can you alter a subplot title location in plotly? 相关联,后者寻求正确对齐子图块的答案。
我在 2 x 2 网格上遇到了类似的子图标题问题。答案很简单
fig.update_annotations(yshift=20)