Shai Zarzewski
Shai Zarzewski

Reputation: 1698

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) enter image description here

Upvotes: 0

Views: 1400

Answers (1)

CT Zhu
CT Zhu

Reputation: 54380

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.

Upvotes: 2

Related Questions