我想使用plt.quiver
以正确的 Angular 显示数组渐变的箭头。
import numpy as np
import matplotlib.pyplot as plt
test_array = np.array([[ 0., 0., 0., 0., 0.],
[ 0., 64., 128., 64., 0.],
[ 0., 127., 255., 127., 0.],
[ 0., 64., 127., 64., 0.],
[ 0., 0., 0., 0., 0.]]).astype(np.float64)
dy,dx = np.gradient(test_array)
theta =np.degrees(np.arctan2(dy,dx))
print(theta)
color = np.sqrt(dy**2 + dx**2)
plt.quiver(dx,dy,color)
plt.show()
但是,如果我打印
theta
:[[ 0. 90. 90. 90. 0. ]
[ 0. 44.77531182 90. 135.22468818 180. ]
[ 0. 0. -90. 180. 180. ]
[ 0. -45. -90. -135. 180. ]
[ 0. -90. -90. -90. 0. ]]
我可以看到第一行的 Angular 是
[90,90,90]
当我使用quiver
绘制渐变时,我可以看到箭头指向相反的方向。我假设 Angular 是逆时针显示的,但它们是顺时针显示的。我想念什么?有人可以解释吗?
最佳答案
将数组视为由plt.quiver
绘制时向下翻转的数组。您可以通过在test_array
中引入不对称性来看到这一点。例如
test_array = np.array([[ 0., 0., 20., 50., 0.],
[ 0., 64., 128., 64., 0.],
[ 0., 127., 255., 127., 0.],
[ 0., 64., 127., 64., 0.],
[ 0., 0., 0., 0., 0.]]).astype(np.float64)
将按照以下方式绘制(注意theta
中的 Angular 和图底部的箭头 Angular ):>>> print(theta)
[[ 0. 81.11934085 76.96664367 125.53767779 180. ]
[ 0. 44.77531182 90. 148.97046189 180. ]
[ 0. 0. -90. 180. 180. ]
[ 0. -45. -90. -135. 180. ]
[ 0. -90. -90. -90. 0. ]]
当您查看
test_array
或theta
时,您可能会认为原点位于左上角。但是matplotlib的起源在左下角。
关于numpy - 使用arctan2进行梯度定向会导致角度翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631756/