How can I make the horizontal seaborn barplot right aligned / mirrored
JavaScript
x
8
1
import matplotlib.pyplot as plt
2
import seaborn as sns
3
4
x = ['x1', 'x2', 'x3']
5
y = [4, 6, 3]
6
sns.barplot(x=y, y=x, orient='h')
7
plt.show()
8
The default horizontal barplot looks like this
I want to have something like this (with proper xticks)
Advertisement
Answer
In order to invert the x axis, you can use:
JavaScript
1
2
1
ax.invert_xaxis()
2
Then, in order to move the labels to the right, you can use:
JavaScript
1
2
1
plt.tick_params(axis = 'y', left = False, right = True, labelleft = False, labelright = True)
2
or, shorter:
JavaScript
1
2
1
ax.yaxis.tight_right()
2
Complete Code
JavaScript
1
12
12
1
import matplotlib.pyplot as plt
2
import seaborn as sns
3
4
x = ['x1', 'x2', 'x3']
5
y = [4, 6, 3]
6
ax = sns.barplot(x=y, y=x, orient='h')
7
8
ax.invert_xaxis()
9
ax.yaxis.tick_right()
10
11
plt.show()
12