Home > database >  How to make y-axis labels center-aligned in seaborn?
How to make y-axis labels center-aligned in seaborn?

Time:06-04

I generate two plots in seaborn which share y-axis. I'm wondering how I can make the shared y-axis labels center-aligned. I am looking for some ideas and improvements. The plot is attached. enter image description here

import seaborn as sns
import matplotlib.pylab as plt
import numpy as np
import string
import random

labels = []

for i in range(10):
   labels.append(''.join(random.choices(string.ascii_lowercase, k=4)))
   labels.append(''.join(random.choices(string.ascii_lowercase, k=7)))     
   
score = np.abs(np.random.randn(20))

fig, axes = plt.subplots(1,2 , figsize=(5,5 ),  sharey=True )

for ii in range(2):   
    ti = sns.barplot(y=[j for j in range(len(score))],x=score, ax=axes[ii],                         
                      orient='h' )    
    ti.set_yticklabels(labels)            
    if ii ==0:
         ti.invert_xaxis()
         ti.yaxis.tick_right()      
            
fig.tight_layout(w_pad=0, pad=1)
plt.show()
    

CodePudding user response:

The ti.set_yticklabels(labels) has options to align horizontally using ha, multi-align text with ma. I have made use of ha and adjusted using the position argument as well. It seems to be aligned. Probably this could also help to design and play with it to get to right format. enter image description here

  • Related