2015-11-30 108 views
4

我有一个二维数组存储每个点的属性值作为其元素:f(x,y) = f[x][y]。现在我想找到这个数组的渐变。我看着np.gradient,但它只是给出了两个数组作为返回,首先是x方向的导数和y方向的第二数组。生成二维数组的梯度图

我想了解如何使用此方法或任何其他方式创建显示2D数组梯度变化的梯度图。
varray是我想创建梯度图的二维数组。以下是我现在可以考虑的唯一事情。我知道应该有巧妙的方法来使用由np.gradient()生成的x gradienty gradient,但我想不起来。 lxly是2D阵列的x和y维度。

vgrad = np.gradient(varray) 
xgrad = vgrad[0] 
x, y = range(0, lx), range(0,ly) 
xi, yi = np.meshgrid(x, y) 
rbf = scipy.interpolate.Rbf(xi, yi, xgrad) 
plt.imshow(v, vmin = np.amin(xgrad), vmax=np.amax(xgrad)) 
plt.colorbar() 
plt.show() 

我想基本上从第一个图像获得第二个图像。第二张图像被描述为σ = \alpha*grad(varray)

使用下面@Mad Physicist所建议的梯度幅度。

vgrad = np.gradient(varray) 
fulgrad = np.sqrt(vgrad[0]**2 + vgrad[1]**2) 
plt.imshow(fulgrad,cmap=plt.get_cmap('hot'), vmin = np.amin(fulgrad),vmax = np.amax(fulgrad)) 
plt.colorbar() 
plt.show() 

像我得到: enter image description here

我解释这个错误来自方程的基本的了解?

所以这里是我的图片。左图:初始2D地图的图像。在右边:梯度图的图像。 @Mad Physicist你认为他们是否与上面类似,只有颜色差异?

enter image description hereenter image description here

+0

你能向我们展示的情节你正在寻找与您的数据来创建类型的例子吗?例如,你想要一个流线图吗? http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.streamplot – ballsatballsdotballs

+1

你只需要使用不同的颜色映射。你得到的图像很好。选择确切的颜色图可能有点困难。最初的预期图像是如何产生的? –

+0

嘿@ballsatballsdotballs,我添加了我从剧情中寻找的东西。最初的情节和情节之后。 –

回答

3

如果你正在寻找的梯度的大小,你可以做

mag = np.sqrt(vgrad[0]**2 + vgrad[1]**2) 

然后绘制mag代替xgrad如上。如果,你想绘制渐变为矢量地图或流情节,这样做

plt.streamplot(xi, yi, vgrad[0], vgrad[1]) 

您还可能有兴趣在斜坡的可视化表示,可以从刚刚绘制三维原始表面获得:

fig = plt.figure() 
ax = fig.gca(projection='3d') 
surf = ax.plot_surface(xi, yi, varray) 
plt.show() 

What is the equivalent of Matlab's surf(x,y,z,c) in matplotlib?http://matplotlib.org/examples/mplot3d/surface3d_demo.html

+0

嘿感谢您的建议,我用你说的梯度幅度,但我没有得到结果接近上述。我也保留了使用量级后得到的结果。 –