为数组中的每组值定义颜色表

时间:2017-03-05 05:59:39

标签: python matplotlib colormap

我有一个包含3组值的数组:

  • 表示0到1之间的值(np.ma.masked_array(array, array > 1.))我想要一个渐变(例如cmap = cm.Greens
  • 对于等于2的值(np.ma.masked_array(array, array != 2.))我希望颜色为红色
  • 对于等于3的值(np.ma.masked_array(array, array != 3.))我希望颜色为灰色

我应该为每组值定义一个色彩映射,然后将它们全部合并到一个色彩映射中吗?如果是这样,我该怎么办?

在这个网站(http://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/Show_colormaps)上,我发现ListedColormapLinearSegmentedColormap等选项可能会有所帮助,但我真的不知道如何使用它来获取我想要的内容

编辑:我做到了,但它不起作用,因为我不知道如何使用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")

1 个答案:

答案 0 :(得分:2)

ListedColormap最好用于离散值,而LinearSegmentedColormap更容易为连续值创建。特别是,如果要使用现有的色彩映射,LinearSegmentedColormap是一个不错的选择。

LinearSegmentedColormap.from_list("name", colors)需要一个颜色列表colors(不是色彩图!)。可以使用现有的色彩图来创建该列表,例如,来自该地图的50种颜色的greens = cm.Greens(np.linspace(0,1, num=50))。对于覆盖相同范围的另一种颜色,我们可以添加相同数量的颜色,例如都是红色或灰色。

以下是一个例子。

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

enter image description here

此处,列表中的颜色在最终色彩图中间隔相等。

另一种方法是指定颜色并附上相应的值。

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)