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 aLinearSegmentedColormap
is more easily created for continuous values. Especially, if an existent colormap shall be used, aLinearSegmentedColormap
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.
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.