Plotly: How to rotate yaxis title in a facet grid?

Question:

enter image description here

Hi there, the issue as you can see is that the title squeeze into together on the right, is there any way to rotate it?
Thanks a lot.

fig = px.box(df, 'deposit_amt', color = 'class', facet_row= 'Age_Group')
Asked By: ZKK

||

Answers:

The correct answer here will most likely depend heavily on how you’ve built your figure. You see, what you’re trying to fix here is in fact the angle of some annotations. And if you’re right that what appears as messed up text in your figure are in fact title of the yaxis, you can just build your plot using a slightly different approach and then use

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

I’ll use the dataset stored in px.data.tips() to show you what I mean. That dataset seems to resemble your real world dataset to a satisfactory degree. It’s at least possible to build this figure out of it:

enter image description here

Now, apply the snippet above and you’ll get:

enter image description here

Complete code:

import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="tip", y="sex", facet_row='time', color = 'time')

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

fig.show()
Answered By: vestland

There is actually a much better solution to this, if you facet by columns and you force it to have 1 plot per row it will format the titles much better.
Note that you might need to adjust the facet_row_spacing

df = px.data.tips()
fig = px.box(df, x="tip", y="sex", facet_col='time', facet_col_wrap=1, color = 'time')
fig.show()

enter image description here

Answered By: fil
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.