Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right align horizontal seaborn barplot

How can I make the horizontal seaborn barplot right aligned / mirrored

import matplotlib.pyplot as plt
import seaborn as sns

x = ['x1', 'x2', 'x3']
y = [4, 6, 3]
sns.barplot(x=y, y=x, orient='h')
plt.show()  

The default horizontal barplot looks like this
enter image description here

I want to have something like this (with proper xticks)
enter image description here

like image 343
Julkar9 Avatar asked Jun 11 '26 22:06

Julkar9


1 Answers

In order to invert the x axis, you can use:

ax.invert_xaxis()

Then, in order to move the labels to the right, you can use:

plt.tick_params(axis = 'y', left = False, right = True, labelleft = False, labelright = True)

or, shorter:

ax.yaxis.tight_right()

Complete Code

import matplotlib.pyplot as plt
import seaborn as sns

x = ['x1', 'x2', 'x3']
y = [4, 6, 3]
ax = sns.barplot(x=y, y=x, orient='h')

ax.invert_xaxis()
ax.yaxis.tick_right()

plt.show() 

enter image description here

like image 134
Zephyr Avatar answered Jun 13 '26 11:06

Zephyr