为数组中的每组值定义一个颜色图 [英] Define a colormap for each set of values in an array

查看:56
本文介绍了为数组中的每组值定义一个颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我是否应该为每组值定义一个颜色图,然后将它们全部合并为一个颜色图?如果是这样,我该如何进行?

在此网站上(

在此,列表中的颜色在最终的颜色图中均等分布.

另一种方法是指定带有相应值的颜色.

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

I have an array with 3 set of values ​​that I want to color:

  • for values between 0 and 1 (np.ma.masked_array(array, array > 1.)) I want a gradient (cmap = cm.Greens for example)
  • for values ​​equal to 2 (np.ma.masked_array(array, array != 2.)) I want the color to be red
  • for values ​​equal to 3 (np.ma.masked_array(array, array != 3.)) I want the color to be gray

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.

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

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. Especially, if an existent colormap shall be used, a LinearSegmentedColormap is a good choice.

The LinearSegmentedColormap.from_list("name", colors) expects a list of colors ,colors (not a colormap!). This list can be created using an existent colormap, e.g. greens = cm.Greens(np.linspace(0,1, num=50)) with 50 colors from that map. For another color to cover the same range we can add the same number of colors, e.g. 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)

这篇关于为数组中的每组值定义一个颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆