2017-07-04 58 views
0

我有以下問題。我有一個7列的陣列。我想要1,2和5列的堆疊直方圖,這樣就有3個橫條堆疊在每列中不同元素的數量上。所以對於第1列,我有9個值,4和1的值與3.在第2列中,我有3個值3和7的值4.在列5中,我有不同數量的值。我有一個appoach,但它非常髒,我只能繪製列1和2.此外,我想繪製一個標籤到不同的酒吧,以顯示他們正在顯示的數字。Python:在numpy數組中創建具有不同數值的堆棧直方圖

bestof10= [4.041970802919708228e-01 4.000000000000000000e+00 4.000000000000000000e+00 6.710000000000000853e+01 5.500000000000000444e-01 4.000000000000000000e+00 6.000000000000000000e+01 
    3.730468750000000000e-01 4.000000000000000000e+00 4.000000000000000000e+00 1.932000000000000171e+02 7.000000000000000666e-01 6.000000000000000000e+00 6.200000000000000000e+01 
    3.626570915619389823e-01 4.000000000000000000e+00 3.000000000000000000e+00 3.900000000000000000e+01 5.000000000000000000e-01 4.000000000000000000e+00 5.300000000000000000e+01 
    3.568320278503046006e-01 4.000000000000000000e+00 4.000000000000000000e+00 8.640000000000000568e+01 6.000000000000000888e-01 7.000000000000000000e+00 6.300000000000000000e+01 
    3.527336860670193808e-01 4.000000000000000000e+00 4.000000000000000000e+00 6.780000000000001137e+01 6.000000000000000888e-01 3.000000000000000000e+00 5.900000000000000000e+01 
    3.521825396825397081e-01 4.000000000000000000e+00 4.000000000000000000e+00 1.568000000000000114e+02 7.000000000000000666e-01 1.000000000000000000e+00 5.700000000000000000e+01 
    3.432835820895522305e-01 3.000000000000000000e+00 4.000000000000000000e+00 8.904999999999999716e+01 6.500000000000000222e-01 7.000000000000000000e+00 4.200000000000000000e+01 
    3.374888691006233121e-01 4.000000000000000000e+00 3.000000000000000000e+00 3.194999999999999929e+01 4.500000000000000111e-01 7.000000000000000000e+00 5.600000000000000000e+01 
    3.364879074658254643e-01 4.000000000000000000e+00 4.000000000000000000e+00 2.430000000000000000e+02 7.500000000000000000e-01 5.000000000000000000e+00 6.100000000000000000e+01 
    3.244781783681214282e-01 4.000000000000000000e+00 3.000000000000000000e+00 8.100000000000001421e+01 6.000000000000000888e-01 6.000000000000000000e+00 5.500000000000000000e+01] 

a = np.bincount(bestof10[:,1].astype(int)) 
ab = np.nonzero(a)[0] 
a = a[ab] 
a = tuple(a) 

b = np.bincount(bestof10[:,2].astype(int)) 
bb = np.nonzero(b)[0] 
b = b[bb] 
b = tuple(b) 

histogramParas=np.column_stack((a,b)) 

N = 2 

ind = np.arange(N) 
width = 0.35 

p1 = plt.bar(ind, histogramParas[0], width, color='r') 
p2 = plt.bar(ind, histogramParas[1], width, color='y',bottom=histogramParas[0]) 

plt.ylabel('Scores') 
plt.title('Scores by group and gender') 
plt.xticks(ind+width/2., ('Radius', 'FRC')) 

plt.show() 

enter image description here

編輯 應該像這樣:

enter image description here

+0

請添加用於創建給定圖像的代碼。 –

+0

我沒有創建圖片,我只是想展示堆疊的酒吧應該如何看起來像 – Varlor

+0

然後你真的有兩個問題:「我如何製作這樣的圖?和「我該如何索引這個數組?」第二個答案是'np.array(x).reshape(7,-1)[[0,1,4]]''。不確定第一個。 –

回答

0

首先,你需要得到你的列表爲2D numpy的陣列。當你知道你有多少列有這個很簡單:

x = np.array(bestof10) 
x = x.reshape(-1, 7) 

所以,現在你有10行7列的二維數組(形狀=(10,7))。所以現在你可以用matplotlib輕鬆繪製你的直方圖。

fig, ax = plt.subplots() 
_ = ax.hist([x[:, 1], x[:, 2], x[:, 5]], 
      stacked=True, normed=False) 

Histogram of columns 1,2 and 5

+0

謝謝你的讚美:)但我只想要第1,2列和第5列中不同元素的編號,就像我編輯中的草圖。 – Varlor

相關問題