python - 将颜色图与 matplotlib 循环器一起使用

标签 python matplotlib

<分区>

我想使用 matplotlib 循环器和来自 palettable 的颜色。

循环仪看起来像这样:

from cycler import cycler
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
                           cycler('linestyle', ['-', '--', ':', '-.'])))

如何用从 palettable 获得的颜色图替换上面的颜色列表?

import palettable
cmap = palettable.colorbrewer.diverging.PRGn_11.mpl_colormap

对于答案,使用 palettable 并不重要,但知道如何使用颜色图很重要。

最佳答案

cycler 需要一个可迭代对象来分配给 'colors'

您可以通过以下方式生成一个:

[plt.get_cmap('jet')(1. * i/n) for i in range(n)]

所以从你原来的例子:

plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
                           cycler('linestyle', ['-', '--', ':', '-.'])))

x = [1,2,3,4]
for i in range(4):
    plt.plot([_ + i for _ in x])

enter image description here

从“jet”颜色图修改列表:

n = 4 # Number of colors
new_colors = [plt.get_cmap('jet')(1. * i/n) for i in range(n)]

plt.rc('axes', 
       prop_cycle=(cycler('color', new_colors) + 
                   cycler('linestyle', ['-', '--', ':', '-.'])))

for i in range(4):
    plt.plot([_ + i for _ in x])

enter image description here

关于python - 将颜色图与 matplotlib 循环器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34914324/

相关文章:

python - 使用python将两个列表中的元素分组到新列表中

python - 消除 Matplotlib/Basemap pcolor 图中的白边

python - matplotlib:改变条形的位置

python - python 中我的颜色条范围是多少?我如何获得句柄?

python - 值错误: could not convert string to float: 'r'

python - 在 Django Q 对象中查找最新日期

python - 包的层次结构

python - Django 以字符串形式获取当前 View 的名称

python - 带有值的堆叠条形图

python - 如何在 python 中从 .obj 文件获取坐标?