简体   繁体   English

使用matplotlib颜色图进行颜色查找

[英]Color lookup with a matplotlib colormap

I need to look up the RGB values for arbitrary numbers from a colormap. 我需要从颜色表中查找任意数字的RGB值。

Assume I have a colormap 'spring' 假设我有一个颜色图'spring'

from matplotlib import cm

c = cm.get_cmap('spring')

And now suppose I have a value from 0 to 1 , I want to pluck the RGB values from c 现在假设我有一个01的值,我想从c提取RGB值

import numpy as np

np.random.seed([3, 1415])
v = np.random.rand(1)

v

0.4449393091533107

How do map that to get the corresponding RGB? 如何映射以获得相应的RGB?

If I look at 如果我看

c._segmentdata

{'blue': ((0.0, 1.0, 1.0), (1.0, 0.0, 0.0)),
 'green': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)),
 'red': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0))}

... I'm at a loss as to what to do. ...我不知所措。

I've looked up this link https://matplotlib.org/users/colormapnorms.html 我已经查看了此链接https://matplotlib.org/users/colormapnorms.html

But this tells me how to do it for a whole 2d matrix. 但这告诉我如何对整个2D矩阵执行此操作。

Unless I misunderstand what you're asking, you can do the following: 除非我误解了您的要求,否则您可以执行以下操作:

c(v[0])

This will give you the following RGBA array: 这将为您提供以下RGBA阵列:

(1.0, 0.44313725490196076, 0.55686274509803924, 1.0)

So if you just want the RGB , you can just index it as necessary: 因此,如果只需要RGB ,则可以根据需要对其进行索引:

>>> c(v[0])[:3]
(1.0, 0.44313725490196076, 0.55686274509803924)

More info in the docs 文档中的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM