matplotlib箭图:RGBA参数无效

2024-06-01 03:53:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了这段代码

from scipy.ndimage import gaussian_filter

U = gaussian_filter(optflow[1][:,:,0], sigma = 10)
V = gaussian_filter(optflow[1][:,:,1], sigma = 10)
U[abs(U) < 0.5] = 0
V[abs(V) < 0.5] = 0

plt.quiver(U[::7, ::7], V[::7, ::7], scale=0.4, scale_units = 'xy', headlength = 7)

#plt.axis('off')
plt.gca().invert_yaxis()
plt.savefig('/home/RR/TEST0.png')
matplotlib.pyplot.clf()

绘制这种形式的矢量场:

我的问题是:如何根据箭头的模块更改箭头的颜色

我试过这个:

%matplotlib inline
%pylab
from scipy.ndimage import gaussian_filter

U = gaussian_filter(optflow[1][:,:,0], sigma = 10)
V = gaussian_filter(optflow[1][:,:,1], sigma = 10)
U[abs(U) < 0.5] = 0
V[abs(V) < 0.5] = 0

M = np.sqrt(U*U+V*V)

plt.quiver(U[::7, ::7], V[::7, ::7], color = cm.inferno(M), scale=0.4, scale_units = 'xy', headlength = 7)

#plt.axis('off')
plt.gca().invert_yaxis()
plt.savefig('/home/RR7g.png')
matplotlib.pyplot.clf()

但是出现了以下错误

Invalid RGBA argument

Tags: fromimportmatplotlibpltscipyabsgaussianfilter
1条回答
网友
1楼 · 发布于 2024-06-01 03:53:03
%matplotlib inline
%pylab
from scipy.ndimage import gaussian_filter
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

U = gaussian_filter(optflow[1][:,:,0], sigma = 10)
V = gaussian_filter(optflow[1][:,:,1], sigma = 10)
U[abs(U) < 0.5] = 0
V[abs(V) < 0.5] = 0

M = np.sqrt(U*U+V*V)
# cmap = plt.cm.get_cmap("inferno", 7).colors
# colors = list((mcolors.to_hex(x) for x in cmap))
plt.quiver(U[::7, ::7], V[::7, ::7], C=M, cmap='inferno', scale=0.4, scale_units = 'xy', headlength = 7)

#plt.axis('off')
plt.gca().invert_yaxis()
plt.savefig('/home/roberto/workspace/TEST/OUTPUT/CORRECTIONCLEAN/TEST0.png')
matplotlib.pyplot.clf()

编辑省略的括号 将2个更改的生成器编辑为列表 编辑3尝试将颜色设置为箭头的大小

相关问题 更多 >