Sau*_*tro 4 python matplotlib grayscale
我一直在尝试按照此处的说明使用从0.(白色)到1.(黑色)的浮点数沿灰度设置多条线的颜色。该函数line.set_color()接受浮点数,但是当我执行以下操作时会出现以下错误plt.show():
ValueError: to_rgba: Invalid rgba arg "1.0"
to_rgb: Invalid rgb arg "1.0"
cannot convert argument to rgb sequence
Run Code Online (Sandbox Code Playgroud)
在这个答案中解释了如何使用plt.cm.RdYlBu(i). 灰度有什么等价物吗?
由于历史原因,matplotlib期望“浮点数”被解释为作为字符串传入的灰度值,这就是您出错的原因。
例如:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), color="0.5")
plt.show()
Run Code Online (Sandbox Code Playgroud)

此外,对于灰度颜色图,只需使用plt.cm.gray或plt.cm.gray_r(反转)。这里有完整的颜色图列表:http : //matplotlib.org/examples/pylab_examples/show_colormaps.html