Измените положение / ориентацию заголовка субплота в Plotly Python - PullRequest
2 голосов
/ 28 марта 2019

Мне нужно поменять название субплота на 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 градусов, чтобы они не перекрывали друг друга. Можно ли решить эту проблему?

1 Ответ

2 голосов
/ 29 марта 2019

Вы можете просто добавить этот код перед строкой 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...