Al777
Al777

Reputation: 9

How to switch x axis with y after pandas groupby plot

Here's an example:

titanic = sns.load_dataset("titanic")
g = titanic.groupby('embark_town').count()['survived']
g.plot(kind='bar')

town x count

I'm trying for a while to make it count x town, how can I do it?

Upvotes: 0

Views: 725

Answers (1)

Grayrigel
Grayrigel

Reputation: 3594

You can supply kind='barh' instead of kind='bar' in the g.plot:

import matplotlib.pyplot as plt
import seaborn as sns

titanic = sns.load_dataset("titanic")
g = titanic.groupby('embark_town').count()['survived']
g.plot(kind='barh')
plt.tight_layout()
plt.show()

Or you can use g.plot.barh()

import matplotlib.pyplot as plt
import seaborn as sns

titanic = sns.load_dataset("titanic")
g = titanic.groupby('embark_town').count()['survived']
g.plot.barh()
plt.tight_layout()
plt.show()

Output : enter image description here

Upvotes: 1

Related Questions