在图像上使用箭袋说明渐变时,箭头非常密集。我如何显示总体趋势而不是每个单独的箭头?
例如,在此图像中显示渐变的箭袋箭头:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image = Image.open('test.png')
array = np.array(image)
array = array[:,:,1]
array = array.astype(float)
dy, dx = np.gradient(array)
plt.figure(figsize=(10, 10))
plt.imshow(array)
plt.quiver(dx, -dy)
plt.show()
添加
Y, X = np.mgrid[0:50:5, 0:50:5]
plt.quiver(X, Y, dx[::5, ::5], -dy[::5, ::5])
产生奇怪的结果
最佳答案
quiver
图有一个参数 scale
。来自 ♦the documentation :
scale: [ None | float ]
Data units per arrow length unit, e.g., m/s per plot width; a smaller scale parameter makes the arrow longer. If None, a simple autoscaling algorithm is used, based on the average vector length and the number of vectors.
将此比例设置为合理的值,也可以让绘图看起来更好。另请检查其他参数,例如 scale_units
和 units
。
import numpy as np
import matplotlib.pyplot as plt
f = lambda x,y, x0,y0, sig: np.exp((-(x-x0)**2- (y-y0)**2)/sig**2)
X,Y = np.meshgrid(np.arange(50), np.arange(50))
array = f(X,Y, 24,24,7.)
dy, dx = np.gradient(array)
n = 3
plt.figure(figsize=(7, 7))
plt.imshow(array)
plt.quiver(X[::n,::n],Y[::n,::n],dx[::n,::n], -dy[::n,::n],
np.sqrt(dx[::n,::n]**2+dy[::n,::n]**2),
units="xy", scale=0.04, cmap="Reds")
plt.show()
关于python - 使用 matplotlib 显示稀疏箭袋箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777513/