Reputation: 7014
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 0
s, 1
s, and 2
s (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?
Upvotes: 9
Views: 16010
Reputation: 699
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()
Upvotes: 11