Python 从 matplotlib 中的颜色图中获取单个颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25408393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:11:03  来源:igfitidea点击:

Getting individual colors from a color map in matplotlib

pythonmatplotlibcolors

提问by LondonRob

If you have a Colormap cmap, for example:

如果您有 Colormap cmap,例如:

cmap = matplotlib.cm.get_cmap('Spectral')

How can you get a particular colour out of it between 0 and 1, where 0 is the first colour in the map and 1 is the last colour in the map?

如何从 0 到 1 之间获取特定颜色,其中 0 是地图中的第一种颜色,而 1 是地图中的最后一种颜色?

Ideally, I would be able to get the middle colour in the map by doing:

理想情况下,我可以通过执行以下操作获得地图中的中间颜色:

>>> do_some_magic(cmap, 0.5) # Return an RGBA tuple
(0.1, 0.2, 0.3, 1.0)

采纳答案by Ffisegydd

You can do this with the code below, and the code in your question was actually very close to what you needed, all you have to do is call the cmapobject you have.

你可以用下面的代码来做到这一点,你的问题中的代码实际上非常接近你所需要的,你所要做的就是调用cmap你拥有的对象。

import matplotlib

cmap = matplotlib.cm.get_cmap('Spectral')

rgba = cmap(0.5)
print(rgba) # (0.99807766255210428, 0.99923106502084169, 0.74602077638401709, 1.0)

For values outside of the range [0.0, 1.0] it will return the under and over colour (respectively). This, by default, is the minimum and maximum colour within the range (so 0.0 and 1.0). This default can be changed with cmap.set_under()and cmap.set_over().

对于范围 [0.0, 1.0] 之外的值,它将返回底色和底色(分别)。默认情况下,这是该范围内的最小和最大颜色(即 0.0 和 1.0)。可以使用cmap.set_under()和更改此默认值cmap.set_over()

For "special" numbers such as np.nanand np.infthe default is to use the 0.0 value, this can be changed using cmap.set_bad()similarly to under and over as above.

对于“特殊”数字,例如np.nan并且np.inf默认值是使用 0.0 值,这可以使用cmap.set_bad()类似于上面的 under 和 over进行更改。

Finally it may be necessary for you to normalize your data such that it conforms to the range [0.0, 1.0]. This can be done using matplotlib.colors.Normalizesimply as shown in the small example below where the arguments vminand vmaxdescribe what numbers should be mapped to 0.0 and 1.0 respectively.

最后,您可能需要对数据进行规范化,使其符合范围[0.0, 1.0]。这可以通过matplotlib.colors.Normalize简单地使用下面的小例子来完成,其中参数vminvmax描述应该分别映射到 0.0 和 1.0 的数字。

import matplotlib

norm = matplotlib.colors.Normalize(vmin=10.0, vmax=20.0)

print(norm(15.0)) # 0.5

A logarithmic normaliser (matplotlib.colors.LogNorm) is also available for data ranges with a large range of values.

对数归一化器 ( matplotlib.colors.LogNorm) 也可用于具有大范围值的数据范围。

(Thanks to both Joe Kingtonand tcaswellfor suggestions on how to improve the answer.)

(感谢Joe Kingtontcaswell关于如何改进答案的建议。)

回答by amaliammr

In order to get rgba integer value instead of float value, we can do

为了得到 rgba 整数值而不是浮点值,我们可以这样做

rgba = cmap(0.5,bytes=True)

So to simplify the code based on answer from Ffisegydd, the code would be like this:

因此,为了根据 Ffisegydd 的答案简化代码,代码将如下所示:

#import colormap
from matplotlib import cm

#normalize item number values to colormap
norm = matplotlib.colors.Normalize(vmin=0, vmax=1000)

#colormap possible values = viridis, jet, spectral
rgba_color = cm.jet(norm(400),bytes=True) 

#400 is one of value between 0 and 1000

回答by Morten

To build on the solutions from Ffisegyddand amaliammr, here's an example where we make CSV representation for a custom colormap:

为了建立在Ffisegyddamaliammr的解决方案的基础上,以下是我们为自定义颜色图制作 CSV 表示的示例:

#! /usr/bin/env python3
import matplotlib
import numpy as np 

vmin = 0.1
vmax = 1000

norm = matplotlib.colors.Normalize(np.log10(vmin), np.log10(vmax))
lognum = norm(np.log10([.5, 2., 10, 40, 150,1000]))

cdict = {
    'red':
    (
        (0., 0, 0),
        (lognum[0], 0, 0),
        (lognum[1], 0, 0),
        (lognum[2], 1, 1),
        (lognum[3], 0.8, 0.8),
        (lognum[4], .7, .7),
    (lognum[5], .7, .7)
    ),
    'green':
    (
        (0., .6, .6),
        (lognum[0], 0.8, 0.8),
        (lognum[1], 1, 1),
        (lognum[2], 1, 1),
        (lognum[3], 0, 0),
        (lognum[4], 0, 0),
    (lognum[5], 0, 0)
    ),
    'blue':
    (
        (0., 0, 0),
        (lognum[0], 0, 0),
        (lognum[1], 0, 0),
        (lognum[2], 0, 0),
        (lognum[3], 0, 0),
        (lognum[4], 0, 0),
    (lognum[5], 1, 1)
    )
}


mycmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', cdict, 256)   
norm = matplotlib.colors.LogNorm(vmin, vmax)
colors = {}
count = 0
step_size = 0.001
for value in np.arange(vmin, vmax+step_size, step_size):
    count += 1
    print("%d/%d %f%%" % (count, vmax*(1./step_size), 100.*count/(vmax*(1./step_size))))
    rgba = mycmap(norm(value), bytes=True)
    color = (rgba[0], rgba[1], rgba[2])
    if color not in colors.values():
        colors[value] = color

print ("value, red, green, blue")
for value in sorted(colors.keys()):
    rgb = colors[value]
    print("%s, %s, %s, %s" % (value, rgb[0], rgb[1], rgb[2]))

回答by prosti

For completeness these are the cmap choices I encountered so far:

为了完整起见,这些是我目前遇到的 cmap 选择:

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, winter, winter_r

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, Ords_r, Ords_Rd PRGn_r, 配对, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples, RdBudr, RdBuGu_rR RdYlBu、RdYlBu_r、RdYlGn、RdYlGn_r、红人、Reds_r、Set1、Set1_r、Set2、Set2_r、Set3、Set3_r、光谱、光谱_r、Wistia、Wistia_r、YlGn、YlGnr、YlGnr、YlGnr、YlGnr、YlBuR、YlGnr、YlBuR afmhot_r,秋天,秋天_r,二进制,binary_r,骨骼,骨骼,bone_r,brg,brg_r,bwr,bwr_r,cividis,cividis_r,cool,cool_r,coolwarm,coolwarm_r,铜,copper_r,cubehelix,cubehelix_r,flag,flag_r,gist_earth,gist_earth,gist_gray、gist_gray_r、gist_heat、gist_heat_r、gist_ncar、gist_ncar_r、gist_rainbow、gist_rainbow_r、gist_stern、gist_stern_r、gist_yarg、gist_yarg_r、gnuplot、gnuplot2、gnuplot、gnuplot2、gnuplot、hnus_fern、gnuplot、hnus_fern_、gnus_vfr_、 jet_r,岩浆,magma_r,nipy_spectral,nipy_spectral_r,海洋,ocean_r,粉红色,pink_r,等离子体,plasma_r,棱镜,prism_r,彩虹,rainbow_r,地震,地震_r,春天,spring_r,夏天,夏天_r,tab10,tab10_r,tab20,tab20 tab20b、tab20b_r、tab20c、tab20c_r、地形、terrain_r、暮光之城、twilight_r、twilight_shifted、twilight_shifted_r、viridis、viridis_r、冬天、winter_rgray_r、hot、hot_r、hsv、hsv_r、inferno、inferno_r、jet、jet_r、岩浆、magma_r、nipy_spectral、nipy_spectral_r、海洋、ocean_r、粉红色、pink_r、等离子、plasma_r、棱镜、prism_r、彩虹、rainbow_r、地震、地震春天,spring_r,夏天,summer_r,tab10,tab10_r,tab20,tab20_r,tab20b,tab20b_r,tab20c,tab20c_r,地形,地形,twilight,twilight_r,twilight_shifted,twilight_shifted_r,冬天,viridis,viridis,gray_r、hot、hot_r、hsv、hsv_r、inferno、inferno_r、jet、jet_r、岩浆、magma_r、nipy_spectral、nipy_spectral_r、海洋、ocean_r、粉红色、pink_r、等离子、plasma_r、棱镜、prism_r、彩虹、rainbow_r、地震、地震春天,spring_r,夏天,summer_r,tab10,tab10_r,tab20,tab20_r,tab20b,tab20b_r,tab20c,tab20c_r,地形,地形,twilight,twilight_r,twilight_shifted,twilight_shifted_r,冬天,viridis,viridis,viridis,viridis_r,冬天,winter_rviridis,viridis_r,冬天,winter_r