简体   繁体   English

为数组中的每组值定义一个颜色图

[英]Define a colormap for each set of values in an array

I have an array with 3 set of values ​​that I want to color: 我有一个要着色的3组值的数组:

  • for values between 0 and 1 ( np.ma.masked_array(array, array > 1.) ) I want a gradient ( cmap = cm.Greens for example) 对于0到1之间的值( np.ma.masked_array(array, array > 1.) )我想要一个渐变( cmap = cm.Greens例如)
  • for values ​​equal to 2 ( np.ma.masked_array(array, array != 2.) ) I want the color to be red 对于等于2的值( np.ma.masked_array(array, array != 2.) )我希望颜色为红色
  • for values ​​equal to 3 ( np.ma.masked_array(array, array != 3.) ) I want the color to be gray 对于等于3的值( np.ma.masked_array(array, array != 3.) )我希望颜色为灰色

Should I define a colormap for each set of values and then merge all of them into one colormap? 我应该为每组值定义一个颜色图,然后将它们全部合并为一个颜色图吗? If so how do I proceed? 如果是这样,我该如何进行?

On this website ( http://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/Show_colormaps ) I found that options like ListedColormap or LinearSegmentedColormap might be helpful but I don't really know how to use it to get what I want. 在这个网站上( http://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/Show_colormaps ),我发现诸如ListedColormapLinearSegmentedColormap类的选项可能会有所帮助,但我真的不知道如何使用它来获取我想要的是。

EDIT: I made that and it doesn't work because I don't know how to use ListedColormap and LinearSegmentedColormap to get what I want 编辑:我做到了,它不起作用,因为我不知道如何使用ListedColormapLinearSegmentedColormap获得我想要的东西

from random import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm
from matplotlib.colors import ListedColormap

n=11

tab = np.array([[random() for i in range(n)] for j in range(n)])
tab[1,2] = 2.
tab[3,4] = 2.
tab[5,6] = 3.
tab[7,8] = 3.

values1 = np.ma.masked_array(tab, tab > 1.)
values2 = np.ma.masked_array(tab, tab != 2.)
values3 = np.ma.masked_array(tab, tab != 3.)

colors1 = cm.Greens
colors2 = ListedColormap(['red'], 'indexed')
colors3 = ListedColormap(['gray'], 'indexed')

colors = np.vstack((colors1, colors2, colors3))
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

print plt.imshow(tab, cmap = mycmap, interpolation="none")

A ListedColormap is best be used for discrete values, while a LinearSegmentedColormap is more easily created for continuous values. ListedColormap最好用于离散值,而LinearSegmentedColormap更易于创建连续值。 Especially, if an existent colormap shall be used, a LinearSegmentedColormap is a good choice. 特别是,如果要使用现有的颜色图,则LinearSegmentedColormap是一个不错的选择。

The LinearSegmentedColormap.from_list("name", colors) expects a list of colors , colors (not a colormap!). LinearSegmentedColormap.from_list("name", colors)需要一个colors列表, colors (不是colormap!)。 This list can be created using an existent colormap, eg greens = cm.Greens(np.linspace(0,1, num=50)) with 50 colors from that map. 可以使用现有的颜色图创建此列表,例如greens = cm.Greens(np.linspace(0,1, num=50))其中包含50种颜色。 For another color to cover the same range we can add the same number of colors, eg all being red or gray. 为了使另一种颜色能够覆盖相同的范围,我们可以添加相同数量的颜色,例如全部为红色或灰色。

An example is below. 下面是一个示例。

from random import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm

n=11
tab = np.array([[random() for i in range(n)] for j in range(n)])
tab[1,2] = 2.
tab[3,4] = 2.
tab[5,6] = 3.
tab[7,8] = 3.

values1 = np.ma.masked_array(tab, tab > 1.)
values2 = np.ma.masked_array(tab, tab != 2.)
values3 = np.ma.masked_array(tab, tab != 3.)

# 50 values for later use from 0 to 1
greens = cm.Greens(np.linspace(0,1, num=50))
# 25 values for later use from 1 to 1.5
greensfill = cm.Greens(np.ones(25))
# 50 values red for later use from 1.5 to 2.5 
red = [(1,0,0,1)]*len(greens)
# 50 values gray for later use from 2.5 to 3.5 
gray = [(.5,.5,.5,1)]*len(greens)

colors = np.vstack((greens, greensfill, red, gray))
# in total we now have 175 colors in the colormap
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

#we now map those 175 colors to the range between 0 and 3.5
im = plt.imshow(tab, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5)
cb = plt.colorbar(im)
cb.set_ticks([0,1,2,3])

plt.show()

在此处输入图片说明

Here, the colors from the list are equally spaced in the final colormap. 在此,列表中的颜色在最终的颜色图中均等分布。

An alternative could be to specify colors accompanied with the respective values. 另一种选择是指定带有相应值的颜色。

colors = [(0, "white"), (1./3.5, "green"), (1.5/3.5, "green"),
          (1.501/3.5, "red"), (2.5/3.5, "red"), (2.501/3.5, "gray"), (1, "gray") ]
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

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

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