Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change subplots title position/orientation in Plotly Python

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?

like image 470
Okroshiashvili Avatar asked Mar 28 '19 13:03

Okroshiashvili


People also ask

How do you change your title position in plotly?

In order to align titles in visualization using plotly module, we are going to use the update_layout() method. Parameters: title: Accepts string value as the title of the visualization. title_x: This parameter is used to align the title in a horizontal motion and accepts a value from 0 to 1.

What is Iplot plotly?

Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

What is XREF in plotly?

By default, text annotations have xref and yref set to "x" and "y" , respectively, meaning that their x/y coordinates are with respect to the axes of the plot.

What is subplot in plotly?

Simple SubplotFigures with subplots are created using the make_subplots function from the plotly. subplots module. Here is an example of creating a figure that includes two scatter traces which are side-by-side since there are 2 columns and 1 row in the subplot layout.


2 Answers

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

like image 63
PieCot Avatar answered Nov 07 '22 19:11

PieCot


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)
like image 33
A Rob4 Avatar answered Nov 07 '22 21:11

A Rob4