2017-06-19 81 views
0

我正在閱讀Excel表格並生成一個numpy數組(data_array) 在這個數組中,每一行都是不同的組(例如:狗,貓,鳥,昆蟲等等......)項目的數量將取決於每個Excel表單) 的列被固定到9個元素爲每個組(年齡,顏色等)Matplotlib繪製未定義數目的軸

現在我可以繪製曲線圖的每一行。在這個例子中,我只繪製了data_Array的元素7。

fig, ax = plt.subplots() 
x,y = data_array.shape 

ind = np.arange(y) 
width = 1 

rects = ax.bar(ind, data_array[7], width, color='r') 

plt.xticks(ind, names, rotation=90) 
plt.ylabel('Area') 
plt.title('Normalized IS')   
plt.show() 

我可以這樣寫:

rects1 = ax.bar(ind, data_array[1], width, color='r') 
rects2 = ax.bar(ind, data_array[2], width, color='r') 
. 
. 
. 
rectsx = ax.bar(ind, data_array[x], width, color='r') 

然後,我可以畫出他們都在同一個圖表英寸 但是,我讀的每個excel都有不同的x個元素。

我想過使用genereting他們:

for i in range(x): 
    rects[i] = ax.bar(ind, data_array[i], width, color='r') 

,但我得到一個錯誤信息。

Traceback (most recent call last):

File "", line 1, in rects[1]=ax.bar(ind, data_array[7], width, color='r') NameError: name 'rects' is not defined

如何生成每個ax.bar?

+0

「但我收到一條錯誤消息」沒有用。如果您需要幫助,則需要包含完整的錯誤回溯。在詢問代碼時,你也不應該使用'...' - 應該怎麼知道''是什麼以及它是否會導致錯誤? – ImportanceOfBeingErnest

回答

2

你的確可以使用一個循環,

for i in range(x): 
    ax.bar(ind, data_array[i], width, color='r') 

這將繪製在彼此的頂部從不同的行中的所有酒吧。

+0

謝謝,這有很大的幫助。不過,我只看到一個情節。另一個我只看到覆蓋層? – daniel

+0

是的,它們彼此重疊。如果你對此不滿意,你需要準確描述你想要情節的外觀。 – ImportanceOfBeingErnest