Matplotlib:箭袋2D阵列

时间:2014-03-25 09:54:37

标签: python matplotlib multidimensional-array

我正在使用python中的箭袋功能。

我想使用箭头创建包含矢量的两个分量的给定2D数组的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

我从未遇到过使用带有matplotlib的VigraArray的问题,所以我不认为这是问题所在。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

事实上,pylab.quiver似乎无法正确处理vigra.VigraArray。以下替代对我有用:

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