python - matplotlib,使用从 0.(白色)到 1.(黑色)的数字沿灰度设置线条的颜色

标签 python matplotlib grayscale

我一直在努力关注the instructions here使用从 0.(白色)到 1.(黑色)的 float 沿灰度设置多条线的颜色。函数 line.set_color() 接受 float ,但是当我执行 plt.show() 时出现以下错误:

ValueError: to_rgba: Invalid rgba arg "1.0"
to_rgb: Invalid rgb arg "1.0"
cannot convert argument to rgb sequence

this answer它解释了如何使用 plt.cm.RdYlBu(i) 来做到这一点。有灰度的等价物吗?

最佳答案

由于历史原因,matplotlib 希望将“ float ”解释为灰度值以作为字符串传入,这就是您出错的原因。

例如:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10), color="0.5")
plt.show()

enter image description here

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

关于python - matplotlib,使用从 0.(白色)到 1.(黑色)的数字沿灰度设置线条的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17239242/

相关文章:

python - 我在 pygame 中的事件循环有问题

python - 如何临时将 Django 站点移动到子目录中(用于测试)?

python - Matplotlib CDF 回到零

python - 在 python 中使用 seaborn 绘图复制绘图

Java:如何获取图像的RGB值?

css - Raphael.js 的彩色图像到灰度图像在 I.E. 中不起作用9

python - 从 Python 访问 Matlab 时规避轮询

python - 如何从 GroupBy.apply() 中删除多索引?

python - Seaborn FacetGrid 堆叠条形图

c++ - 如何在 OpenCv 中将 cv::Mat 转换为灰度?