【问题标题】:Change subplots title position/orientation in Plotly Python在 Plotly Python 中更改子图标题位置/方向
【发布时间】:2019-03-28 13:58:45
【问题描述】:

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

【问题讨论】:

  • 运行时没有重叠。你介意发张照片吗?
  • 在这个示例代码中,没有重叠,但如果我们增加子情节标题的长度,它们肯定会重叠。是否有可能以某种方式将子情节标题旋转 90 度
  • 我建议你提供一个重叠的例子。然后,如果将标题旋转 90 度,它可能会与 trace3 中的条重叠。您是否考虑过使用单独的 yaxis 并使用标题作为 y 标签?
  • 我只需要旋转子情节标题。只要告诉我是否可能,如果可能的话,我该怎么做?

标签: python plotly plotly-dash plotly-python


【解决方案1】:

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

这就是你得到的:

【讨论】:

    【解决方案2】:

    此答案与Can you alter a subplot title location in plotly? 相关联,该Can you alter a subplot title location in plotly? 寻求正确对齐子图块的答案。

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

    fig.update_annotations(yshift=20)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 2020-05-14
      • 1970-01-01
      • 2022-06-11
      • 2015-07-26
      • 1970-01-01
      • 2019-06-25
      • 2020-08-07
      相关资源
      最近更新 更多