2014-12-02 76 views
0

我正在使用python,我是新的。我想從我的代碼繪製兩個數組,其中之一是情節正確,但對方一引發我一個錯誤在python上繪製數組時出錯

/usr/lib64/python2.7/site-packages/matplotlib/axes.py:4511: RuntimeWarning: invalid value encountered in true_divide 
c /= np.sqrt(np.dot(x, x) * np.dot(y, y)) 
Traceback (most recent call last): 
File "./track-multiple.py", line 169, in <module> 
ax2.xcorr(x2, y2) 
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 4508, in xcorr 
c = np.correlate(x, y, mode=2) 
File "/usr/lib64/python2.7/site-packages/numpy/core/numeric.py", line 871, in correlate 
return multiarray.correlate2(a, v, mode) 
ValueError: object too deep for desired array 

的代碼是下一個:

x1 = np.arange(0,len(colunas[0],1) 
y1 = columnas[0] 
x2 = np.arange(0,len(filas)) 
y2 = filas 

fig = plt.figure() 

ax1 = fig.add_subplot(211) 
ax1.xcorr(x1,y1) 
ax1.axhline(0, color='black', lw=2) 

ax2 = fig.add_subplot(212, sharex=ax1) 
ax2.xcorr(x2,y2) 
ax2.axhline(0, color='blue', lw=2) 

plt.show() 

我認爲這個問題是由於他的維度而導致的,或者我濫用了xcorr方法。如果我做一個打印上filas和columnas我得到這個:

filas = 
[[0] 
[0] 
[0] 
[0] 
[0] 
[0] 
[0] 
[0] 
[0] 
[0] 
[0] 

... 

[0] 
[0] 
[0] 
[0] 
[0]] 
columnas = 
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0]] 

兩個陣列用OpenCV的庫中創建這些句子:

columnas = cv2.reduce(fgmask,0,cv.CV_REDUCE_MAX) 
filas = cv2.reduce(fgmask,1,cv.CV_REDUCE_MAX) 

等待的想法!

PD:OK,我覺得如何修正這個錯誤,我只是轉換陣列filas有:

filas = filas[:,0] 

但我不知道錯誤的確切原因,以及修復,任何人都可以回答我的問題?

回答

0

您的變量類型有混淆。 x1x2是N和M長的1D numpy陣列。 filas是1長列表的N長列表。 columnas是一個包含M長列表的長列表。

x1 = np.arange(0,len(colunas[0],1) # N long numpy array 
y1 = columnas[0]      # N long list 
x2 = np.arange(0,len(filas))   # M long numpy array 
y2 = filas       # M long list of 1 long lists 

Python列表和numpy數組是不同的!您可以使用列表列表模擬Python中的二維數組,但您無法簡單地垂直切片(即獲得一列)。但這正是你需要的。

我想你應該決定是否要使用Python或Numpy變量類型並且與你的選擇一致。

Python的方法:

x1 = range(0,len(colunas[0],1)  # N long list 
y1 = columnas[0]      # N long list 
x2 = range(0,len(filas))    # M long list 
y2 = [item[0] for item in filas]  # M long list 

在這裏的最後一行,您遍歷filas(M環路),併爲每個項目(單列表),您所收集的單列表內的號碼。

的numpy的方法:

x1 = np.arange(0,len(colunas[0],1) # N long numpy array 
y1 = np.array(columnas[0])   # N long numpy array 
x2 = np.arange(0,len(filas))   # M long numpy array 
y2 = np.array(filas)[:,0]   # M long numpy array 

在這裏的最後一行,您從您的filas名單列表的2D numpy的陣列,並採取了先進的陣列切片數組是不允許的第一列爲列表。

+0

我忘了謝謝你,對我有用:D – 2015-04-16 21:33:24