简体   繁体   中英

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?

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 : 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM