使用 matplotlib 显示稀疏箭袋箭头
display sparse quiver arrows using matplotlib
在图像上使用箭袋说明渐变时,箭头非常密集。我如何显示总体趋势而不是每个单独的箭头?
例如,在此图像中显示渐变的颤动箭头:
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()
在图像上使用箭袋说明渐变时,箭头非常密集。我如何显示总体趋势而不是每个单独的箭头?
例如,在此图像中显示渐变的颤动箭头:
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()