2010-08-05 107 views
1

我想在matplotlib..so中的堆疊條形圖中交替顏色,因爲根據它們的類型,我可以獲得不同的顏色。我有兩種類型,只是它們不會定期交替。所以我需要在選擇顏色之前檢查它們的類型。如何在matplotlib中堆疊條形圖中交替顏色?

問題是它是有條件的。我在數組中提供了這個類型,但是在plt.bar(.............)的層次上沒有辦法做到這一點......我想。

p1 = plt.bar(self.__ind, 
        self.__a, 
        self.__width, 
        color='#263F6A') 

p2 = plt.bar(self.__ind, 
        self.__b, 
        self.__width, 
        color='#3F9AC9', 
        bottom = self.__arch) 

p3 = plt.bar(self.__ind, 
        self.__c, 
        self.__width, 
        color='#76787A', 
        bottom = self.__a + self.__b) 

自我.__和自我.__ B和自我.__ c是,我需要在同一圖中繪製的所有數據列表和我上面提到的列表中的每個元素的類型另一個列表。 我只想知道如何根據類型列表提供的類型更改圖表的顏色,同時將所有條形圖保留在一個繪圖中。

回答

4

我當你說self.__a是一個列表困惑 - 當我嘗試繪製一個列表:

In [19]: plt.bar(1,[1,2,3], 0.1, color='#ffcc00') 

我得到

AssertionError: incompatible sizes: argument 'height' must be length 1 or scalar 

不過,你所能做的就是情節你的價值觀在一個循環:

# Setup code here... 

indices = [1,2,3,4] 
heights = [1.2, 2.2, 3.3, 4.4] 
widths = [0.1, 0.1, 0.2, 1] 
types = ['spam', 'rabbit', 'spam', 'grail'] 


for index, height, width, type in zip(indices, heights, widths, types): 
    if type == 'spam': 
     plt.bar(index, height, width, color='#263F6A') 
    elif type == 'rabbit': 
     plt.bar(index, height, width, color='#3F9AC9', bottom = self.__arch) 
    elif type == 'grail': 
     plt.bar(index, height, width, color='#76787a', bottom = 3) 
+0

嘿嘿,謝謝... ... 我適應它...它的工作原理ñ icely :) – Atlas 2010-08-06 11:12:09

+2

不客氣,記住,你應該點擊答案左邊的複選標記標記接受的答案 - 如果你發現答案有用,你也應該投票。 – 2010-08-06 12:51:55

相關問題