matplotlib颤抖:无效的RGBA参数

时间:2020-04-03 09:31:06

标签: python matplotlib rgba

我写了这段代码

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

1 个答案:

答案 0 :(得分:1)

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

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