Skip to content
Advertisement

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement