从matplotlib中的给定色彩映射创建颜色生成器

时间:2010-06-10 16:18:14

标签: python matplotlib color-mapping

我有一系列线条,每条线条都需要用单独的颜色绘制。每条线实际上由几个数据集(正面,负面区域等)组成,因此我希望能够创建一个生成器,在一个光谱中一次提供一种颜色,例如{{1} } map shown here

我找到了以下作品,但它看起来非常复杂,更重要的是难以记住,

gist_rainbow

此外,它不包括from pylab import * NUM_COLORS = 22 mp = cm.datad['gist_rainbow'] get_color = matplotlib.colors.LinearSegmentedColormap.from_list(mp, colors=['r', 'b'], N=NUM_COLORS) ... # Then in a for loop this_color = get_color(float(i)/NUM_COLORS) 地图中的颜色范围,我必须重新定义地图。

也许发电机不是最好的方法,如果是这样,接受的方式是什么?

1 个答案:

答案 0 :(得分:25)

要索引特定颜色图的颜色,您可以使用:

import pylab
NUM_COLORS = 22

cm = pylab.get_cmap('gist_rainbow')
for i in range(NUM_COLORS):
    color = cm(1.*i/NUM_COLORS)  # color will now be an RGBA tuple

# or if you really want a generator:
cgen = (cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS))