从matplotlib中的给定颜色图创建颜色生成器 [英] Create a color generator from given colormap in matplotlib
问题描述
我有一系列线,每条线都需要用单独的颜色绘制.每条线实际上由几个数据集(正区域、负区域等)组成,因此我希望能够创建一个生成器,该生成器将在光谱中一次提供一种颜色,例如 gist_rainbow
地图 此处显示.
I have a series of lines that each need to be plotted with a separate colour. Each line is actually made up of several data sets (positive, negative regions etc.) and so I'd like to be able to create a generator that will feed one colour at a time across a spectrum, for example the gist_rainbow
map shown here.
我找到了以下作品,但看起来很复杂,更重要的是难以记住,
I have found the following works but it seems very complicated and more importantly difficult to remember,
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)
而且,它没有覆盖gist_rainbow
地图中的颜色范围,我必须重新定义一个地图.
Moreover, it does not cover the range of colours in the gist_rainbow
map, I have to redefine a map.
也许生成器不是执行此操作的最佳方式,如果是这样,可接受的方式是什么?
Maybe a generator is not the best way to do this, if so what is the accepted way?
推荐答案
要为特定颜色表中的颜色编制索引,可以使用:
To index colors from a specific colormap you can use:
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))
这篇关于从matplotlib中的给定颜色图创建颜色生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!