2014-03-25 105 views
1

我在Python中顫抖函數掙扎。Matplotlib:顫抖二維數組

我想創建一個給定的二維數組的2D矢量字段,其中包含使用顫抖的向量的兩個組件。

鑑於數組b:

>>> b 
VigraArray(shape=(512, 512, 2), axistags=x y c, dtype=float32, data= 
[[[ 0.59471679 0.51902866 0.38904327 ..., -0.56878477 -0.50834674 
-0.48382956] 
[ 0.58222073 0.50713873 0.37990916 ..., -0.56091702 -0.50057167 
-0.47613338] 
[ 0.53815156 0.46551338 0.34787226 ..., -0.54245669 -0.48314109 
-0.45911735] 
..., 

,並使用顫抖:

>>> X,Y = meshgrid(range(b.shape[0]),range(b.shape[1])) 
>>> quiver(X,Y,b[...,0],b[...,1]) 

回報:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2892, in quiver 
    ret = ax.quiver(*args, **kw) 
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6641, in quiver 
    q = mquiver.Quiver(self, *args, **kw) 
File "/usr/lib/pymodules/python2.7/matplotlib/quiver.py", line 419, in __init__ 
self.set_UVC(U, V, C) 
File "/usr/lib/pymodules/python2.7/matplotlib/quiver.py", line 463, in set_UVC 
    U = ma.masked_invalid(U, copy=False).ravel() 
File "/usr/lib/python2.7/dist-packages/numpy/ma/core.py", line 3969, in ravel 
    r._mask = ndarray.ravel(self._mask).reshape(r.shape) 
File "/usr/lib/python2.7/dist-packages/vigra/arraytypes.py", line 1308, in reshape 
    res = numpy.ndarray.reshape(self, shape, order) 
TypeError: an integer is required 

我從來沒有使用VigraArray與matplotlib的問題,所以我不認爲這是問題。感謝幫助!

+0

你試過'b [:,0]'而不是'b [...,0]'? – Schorsch

+0

我試過'b [:,:,0]','b [:,0]'形狀錯了,但是同樣的錯誤 – l4l1lu

回答

0

事實上,pylab.quiver似乎沒有正確處理vigra.VigraArray。下面的替換爲我工作:

b[...,0] = numpy.array(b[...,0]) 
b[...,1] = numpy.array(b[...,1])