gpt4 book ai didi

python - 在 Plotly Python 中更改子图标题位置/方向

转载 作者:太空宇宙 更新时间:2023-11-04 00:02:02 25 4
gpt4 key购买 nike

我需要在 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 度,这样它们就不会重叠在彼此。有可能解决这个问题吗?

最佳答案

您只需在 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')

这就是你得到的: enter image description here

关于python - 在 Plotly Python 中更改子图标题位置/方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55399439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com