Plotly: How to rotate yaxis title in a facet grid?
Question:
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:
Now, apply the snippet above and you’ll get:
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()
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()
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:
Now, apply the snippet above and you’ll get:
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()
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()