1

I made a facetted plotly histogram like so:

fig = px.histogram(df, 
                   x = "x column", 
                   color = "colour column",  
                   facet_col = 'facet column')
fig.update_xaxes(title='')
fig.update_layout(xaxis2=dict(title='lonnnnnnnnnnng x axis title'))
for anno in fig['layout']['annotations']:
    anno['text']=''
fig.update_layout({'title' : 'title'})

I got the figure below. Could someone explain how to center the xaxis2 label ('lonnnnnnnnnnng x axis title') as shown in the pic below?

Thanks!

enter image description here

Tim Kirkwood
  • 598
  • 2
  • 7
  • 18

2 Answers2

0

You changed the title of xaxis to empty, and then you used the title xaxis2, you can shortcut by setting the title to 'lonnnnnnnnnnng x axis title'. It will be like this:

fig.update_xaxes(title='lonnnnnnnnnnng x axis title')

No need for the second line.

Hamzah
  • 8,175
  • 3
  • 19
  • 43
0

Basically the same as your approach, but to center it, the number of facet graphs is the number of variables in the facet, so a loop process is used. The central position is set to the second by truncating the number of graphs by half. If you want to set it even closer to the center, the only way to do so is to use the annotation function. My code is created using the examples in this reference. An additional way to disable the x-axis title, other than your method, is to empty the label of the x-axis variable. This technique was inspired by this answer.

import math
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color='sex', facet_col="day", labels={'total_bill':''})

for i,x in enumerate(df['day'].unique(),start=1):
    if i == math.floor(len(df['day'].unique()) / 2):
        facet = 'xaxis{}'.format(i)
        fig.layout[facet].title = title='lonnnnnnnnnnng x axis title'
    
fig.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32