简体   繁体   中英

matplotlib changing barplot xtick labels and sorting the order of bars

I have a dataset that i'm trying to plot, this is how I plot it:

   for i in range(0,5):
    plt.subplot2grid((1,5),(0,i))
    df.Survived[df.SibSp == i].value_counts().plot(kind='bar')
    title(str(i))

the values in X (survived) are 0 or 1 and i'm plotting the value count of them. I'm trying to change the xSticks to "survived"\\"not survived", how can I do it?

and I'm also trying to sort the xSticks at all graphs at the same way (as you can see in the pic the default is to sort according to ycount, which is causing the x's to be 0-1 at the first graph and 1-0 at the second graph) 在此处输入图片说明

I am not sure how your data looks like, but I assume 1 means survived and 0 means dead, and there are no other values other than 0 and 1. If so, you need a few small changes:

for i in range(0,5):
    plt.subplot2grid((1,5),(0,i))
    ax=df.Survived[df.SibSp == i].value_counts().ix[[0,1]].plot(kind='bar')
    ax.set_xticklabels(['alive', 'dead']) 
    title(str(i))

And you should be ready to go.

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