2011-03-09 56 views
0

我試圖繪製從numpy的陣列的各列求和而獲得數組值。 在Win XP,Python 2.5的工作,matplotlib-1.0.1,numpy的-1.5.1,PIL-1.1.7 下面是代碼:pyplot陣列的崩潰

import Image 
import numpy as np 
import matplotlib.pyplot as plt 
im = Image.open("tish.pnm") 
im = im.convert("1") # convert necessary to import into numpy array 
pixels = im.getdata() 
n = len(pixels) 
data = np.reshape(pixels, im.size) 
sums = {} 
#sum the range of column values 
for i in range(0, data.shape[0]): 
sums[i] = data[:,i].sum() 
#this is where I can't get pyplot to work 
plt.plot(sums) # this crashes with a "ValueError: need more than 0 values to unpack" 
plt.plot(i,sums) #crashes with the same error 

當我做了一個 「打印總和」 我得到數據如:

{0: 705330, 1: 667845, 2: 693855, 3: 663000, 4: 699210, 5: 660705, 6: 686970, 7: 662490, 8: 697425, 9: 660450, 10: 688500, 11: 663510,...2913:65295} 

我在做什麼錯?

回答

0

還應當指出的是,除了這個事實,你正試圖繪製一個字典,而不是一個numpy的數組(這就是爲什麼你的代碼生成錯誤),你可以達到同樣的效果,而無需使用顯式通過使用

sums = np.sum(data,axis=0) 

,然後蟒蛇循環使用

plt.plot(sums) 

一般繪製的數據,應避免將此類數據轉換爲字典擺在首位,因爲你的密鑰值僅indicies,這隱含在numpy數組中。

你也應該注意到,在原始總和的配方,你遍歷一個範圍大小data.shape[0](數據行數),但後來在和你 服用總和爲每列。如果data.shape[1] < data.shape[0],你會得到一個錯誤索引之前,你甚至打你的腳本的繪圖部分。

+0

謝謝位有關加載的金額到一個數組是我需要的,但沒有找到。 – user652221 2011-03-09 20:50:54