6

I need to change subplot title in python plotly, namely, rotate it by 90 degrees. I tried hard but without any success.

Here is my code

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')

So, I want to rotate 'first_title', 'second_title' and 'third_title' by 90 degrees so that they do not overlap at each other. Is it possible to tackle this problem?

rpanai
  • 12,515
  • 2
  • 42
  • 64
Okroshiashvili
  • 3,677
  • 2
  • 26
  • 40
  • there is no overlapping when I run it. Do you mind to post a pic? – rpanai Mar 28 '19 at 14:22
  • In this example code, there is no overlap but if we increase the length of sub-plot titles they'll surely overlap. Is it possible someway to rotate sub-plot titles not necessarily by 90 degrees – Okroshiashvili Mar 28 '19 at 16:04
  • I suggest you to provide an example with overlap. Then if you rotate the title by 90 degrees it could overlap the bars in trace3. Have you consider to use separate yaxis with using the title as y label? – rpanai Mar 28 '19 at 17:23
  • 1
    I just need to rotate sub-plot title. Just tell me if it possible or not and if possible how can I do that? – Okroshiashvili Mar 29 '19 at 05:45

3 Answers3

7

you can simply add this code before the pyo.plot(fig, filename='file.html') line:

for annotation in fig['layout']['annotations']: 
    annotation['textangle']=-90

However, this will cause subplot titles overlapping the main title, so I suggest you to remove it. This is the final code:

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')

And this is what you get: enter image description here

PieCot
  • 3,564
  • 1
  • 12
  • 20
4

This answer is linked to Can you alter a subplot title location in plotly? which seeks an answer to getting the subplot tiles aligned correctly.

I had a similar problem with subplot titles on a 2 x 2 grid. The answer was simply

fig.update_annotations(yshift=20)
A Rob4
  • 1,278
  • 3
  • 17
  • 35
0

You could also update text angles in one line

fig.update_annotations(textangle=-90)
amance
  • 883
  • 4
  • 14