簡體   English   中英

Matplotlib堆積條形圖

[英]Matplotlib stacked bar chart

嗨,我是matplotlib的新手,但我正在嘗試繪制堆積的條形圖。 我的酒吧沒有堆疊,而是相互重疊。

這是我要存儲數據的字典。

eventsDict = {
'A' : [30.427007371788505, 3.821656050955414], 
'B' : [15.308879925288613, 25.477707006369428], 
'C' : [10.846066723627477, 1.910828025477707], 
'D' : [0.32586881793073297, 0.6369426751592357],
'E' : [3.110656307747332, 11.464968152866243], 
'F' : [8.183480040534901, 1.910828025477707], 
'G' : [3.048065650644783, 16.560509554140125], 
'H' : [9.950920976811652, 4.45859872611465]
}

我的堆疊條形圖有兩個條形。 第一個包含列表中第一個值的所有數據,第二個包含列表中所有的第二個值。 (列表是字典中的值)

首先,我將字典轉換為元組:

allEvents = list(self.eventsDict.items()) 

這會將字典轉到此列表:

all Events = [('A', [30.427007371788505, 3.821656050955414]), ('B', [15.308879925288613, 25.477707006369428]), ('C', [10.846066723627477, 1.910828025477707]), ('D', [0.32586881793073297, 0.6369426751592357]), ('E', [3.110656307747332, 11.464968152866243]), ('F', [8.183480040534901, 1.910828025477707]), ('G', [3.048065650644783, 16.560509554140125]), ('H', [9.950920976811652, 4.45859872611465])]

這是我繪制的地方:

    range_vals = np.linspace(0, 2, 3)
    mid_vals = (range_vals[0:-1] + range_vals[1:]) * 0.5
        colors = ['#DC7633', '#F4D03F', '#52BE80', '#3498DB', '#9B59B6', '#C0392B', '#2471A3', '#566573', '#95A5A6']
        x_label = ['All events. %s total events' % (totalEvents), 'Corrected p-value threshold p < %s. %s total events' % (self.pVal, totalAdjusted)]

    #Turn the dict to a tuple. That way it is ordered and is subscriptable.
    allEvents = list(self.mod_eventsDict.items())
    #print (allEvents)

    #Use below to index:
    #list[x] key - value pairing
    #list[x][0] event name (key)
    #list[x][1] list of values [val 1(all), val 2(adjusted)]

    #Plot the Top bar first
    plt.bar(mid_vals, allEvents[0][1], color = colors[0], label = allEvents[0][0])

    #Plot the rest
    x = 1
    for x in range(1, 20):
        try:
            plt.bar(mid_vals, allEvents[x-1][1], bottom =allEvents[x-1][1], color = colors[x], label = allEvents[x][0])           
            x = x + 1
        except IndexError:
            continue


    plt.xticks(mid_vals) # for classic style
    plt.xticks(mid_vals, x_label) # for classic style

    plt.xlabel('values')
    plt.ylabel('Count/Fraction')
    plt.title('Stacked Bar chart')
    plt.legend()
    plt.axis([0, 2.5, 0, 1])
    plt.show()

這是圖形輸出。 理想情況下,堆疊時它們的總和應為1。 我將它們都做成一個整體的一部分,以使兩個條的高度相同。 但是,它們只是彼此重疊。 另外,請注意,堆棧在字典上的名稱與其標簽不同。

堆疊條形圖輸出

請幫我調試!

您需要設置不同的bottom -這會告訴matplotlib將要繪制的條形圖的底部放置在哪里,因此它必須是之前的條形圖的所有高度的總和。

例如,您可以使用如下列表跟蹤條的當前高度:

current_heights = [0] * 20
for x in range(20):
    try:
        plt.bar(mid_vals, allEvents[x][1], bottom=current_heights[x], color=colors[x], label=allEvents[x][0])           
        x = x + 1
        current_heights[x] += allEvents[x][1] #increment bar height after plotting
    except IndexError:
        continue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM