简体   繁体   English

matplotlib更改barplot xtick标签并排序条形顺序

[英]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. X(存活)中的值是0或1,我正在绘制它们的值计数。 I'm trying to change the xSticks to "survived"\\"not survived", how can I do it? 我正在尝试将xSticks更改为“幸存” \\“未幸存”,我该怎么办?

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) 并且我还尝试以相同的方式对所有图的xSticks进行排序(如您在图片中看到的那样,默认是根据ycount进行排序,这导致x在第一张图上为0-1,而1在x上为1在第二张图上为-0) 在此处输入图片说明

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: 我不确定您的数据是什么样子,但是我假设1表示生存,0表示死亡,并且除了0和1之外没有其他值。如果是这样,则需要一些小的更改:

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. 而且您应该准备好出发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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