将颜色图与matplotlib循环仪一起使用 [英] Use colormaps along with matplotlib cycler

查看:53
本文介绍了将颜色图与matplotlib循环仪一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有调色板颜色的 matplotlib 循环器.

I would like to use the matplotlib cycler with colors from palettable.

循环器看起来像这样:

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

如何用我从可调色表中获得的颜色图替换上面的颜色列表?

How do I replace the color list above with the color map I obtain from palettable?

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

对于答案,使用可调色板并不重要,但知道如何使用颜色图很重要.

For the answer, it is not critical to use palettable, but it is important to know how to use a colormap.

推荐答案

骑自行车的人需要迭代器才能分配给'colors'.

cycler needs an iterable to be assigned to 'colors'.

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

Here is a way you could generate one:

[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])

从喷射"色彩图的修改列表:

To the modified list from 'jet' colormap:

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])

这篇关于将颜色图与matplotlib循环仪一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆