首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改Plotly Python中的子图标题位置/方向

更改Plotly Python中的子图标题位置/方向
EN

Stack Overflow用户
提问于 2019-03-28 21:58:46
回答 2查看 6.3K关注 0票数 5

我需要在python中更改subplot标题,即将其旋转90度。我努力了,但没有成功。

以下是我的代码

代码语言:javascript
运行
复制
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度,这样它们就不会相互重叠。有没有可能解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-30 02:12:59

您可以简单地在pyo.plot(fig, filename='file.html')行之前添加以下代码:

代码语言:javascript
运行
复制
for annotation in fig['layout']['annotations']: 
    annotation['textangle']=-90

但是,这会导致子图标题与主标题重叠,因此我建议您删除它。这是最终的代码:

代码语言:javascript
运行
复制
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')

这就是你所得到的:

票数 6
EN

Stack Overflow用户

发布于 2021-03-10 17:02:33

此答案链接到Can you alter a subplot title location in plotly?,它寻求正确对齐子图平铺的答案。

我在2x2网格上的子图标题也有类似的问题。答案很简单

代码语言:javascript
运行
复制
fig.update_annotations(yshift=20)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55399439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档