从 matplotlib 中给定的颜色图创建颜色生成器

发布于 2024-09-05 07:25:56 字数 601 浏览 3 评论 0原文

我有一系列线条,每条线条都需要用单独的颜色绘制。每条线实际上由多个数据集(正区域、负区域等)组成,因此我希望能够创建一个生成器,在整个光谱中一次提供一种颜色,例如 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)

而且它没有涵盖 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)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2024-09-12 07:25:56

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

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文