python - 使用 matplotlib 显示稀疏箭袋箭头

标签 python matplotlib gradient

在图像上使用箭袋说明渐变时,箭头非常密集。我如何显示总体趋势而不是每个单独的箭头?

例如,在此图像中显示渐变的箭袋箭头:

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()

enter image description here enter image description here

而我希望箭头更大、更稀疏,如下所示: enter image description here

添加

Y, X = np.mgrid[0:50:5, 0:50:5]
plt.quiver(X, Y, dx[::5, ::5], -dy[::5, ::5])

产生奇怪的结果

enter image description here

最佳答案

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_unitsunits

enter image description here

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/

相关文章:

user-interface - Flutter 如何绘制自定义渐变

python - 在Python/R中使用SLINK后分离集群

python - 如何找到最接近一系列 n*0.1 的反对数(10base)的每个整数(在 Python 中)

flutter - 如何在 Flutter 中创建带条纹的背景

android - 如何在 android 中创建自定义渐变?

python - 使用 scipy.signal.dstep 绘制离散传递函数

python - 如何在Python中使用多处理定期调用函数?

Python:将字符串存储到数组中似乎会移动字符串索引?

python - matplotlib 以面向对象的方式设置颜色、样式

python - PyQt 对话框中的 matplotlib 十字光标不显示