Q:

Specifying colours when using matplotlib's pcolormesh

Using matplotlib, I want to be able to specify exact colors with pcolormesh. Here is what I have tried
import numpy as np
from matplotlib import pyplot as plt

imports at the top
X = np.linspace(0,1,100)
Y = np.linspace(0,1,100)
X,Y = np.meshgrid(X,Y)
Z = (X**2 + Y**2) < 1.
Z = Z.astype(int)
Z += (X**2 + Y**2) < .5

set up a bunch of fake data. Z is only 0s, 1s, and 2s (this is like my real problem).
plt.pcolormesh(X,Y,Z,color=[(1,1,0),(0,0,1),(1,0,1)])

call pcolormesh with a color argument in a vain attempt to get a yellow, blue, and magenta plot. In fact, I got the default colors!

My question is: how do I call pcolormesh to get the first area to be yellow, the second blue, and the third magenta?

A:

One way is to use a custom colormap:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import colors as c

X = np.linspace(0,1,100)
Y = np.linspace(0,1,100)
X,Y = np.meshgrid(X,Y)
Z = (X**2 + Y**2) < 1.
Z = Z.astype(int)
Z += (X**2 + Y**2) < .5

cMap = c.ListedColormap(['y','b','m'])

plt.pcolormesh(X,Y,Z,cmap=cMap)
plt.show()

