2014-06-07 43 views
0

我有我想要繪製, 我這是怎麼繪製它的數據集:matplotlib改變barplot XTICK標籤和排序巴的

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)) 

的值X(存活)爲0或1,我正在繪製它們的數值。 我試圖將xSticks更改爲「存活」\「不存活」,我該怎麼辦?

我也試圖按照相同的方式在所有圖形上對xSticks進行排序(正如您在圖片中看到的,缺省值是根據ycount進行排序,這會導致x的值爲0-1)第一個圖和1-0在第二個圖) enter image description here

回答

2

我不知道你的數據是怎麼樣的,但我假設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)) 

而你應該準備好去。

+0

謝謝!那工作。如果dead = 0且alive = 1,ax.set_xticklabels(['dead','alive'])應該是ax.set_xticklabels(['alive','dead']) –