3

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')
vestland
  • 55,229
  • 37
  • 187
  • 305
ZKK
  • 627
  • 1
  • 10
  • 16
  • Try `fig.update_yaxes(tickangle = 90)` – Karthik Sep 17 '20 at 12:41
  • sorry, that does not work as this is actually the title of yaxis. I didn't find any syntax for the axis title which confuses me. – ZKK Sep 17 '20 at 12:42
  • Does this answer your question? [Change subplots title position/orientation in Plotly Python](https://stackoverflow.com/questions/55399439/change-subplots-title-position-orientation-in-plotly-python) – Karthik Sep 17 '20 at 13:02

2 Answers2

4

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()
vestland
  • 55,229
  • 37
  • 187
  • 305
4

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

fil
  • 49
  • 5