You need to mask imdata , optionally a :
import numpy as np import matplotlib.pyplot as plt a = np.array([3, 5, 10, np.inf, 5, 8]) imdata = np.ma.masked_invalid(np.atleast_2d(a)) cmap = plt.cm.hot cmap.set_bad('b', 1) fig, ax = plt.subplots() im = ax.pcolormesh(imdata, cmap=cmap) plt.colorbar(im) plt.show()

If you look at imdata in an interactive session, you will see
In [185]: imdata Out[185]: masked_array(data = [[ 3. 5. 10. inf 5. 8.] [ 3. 5. 10. inf 5. 8.]], mask = False, fill_value = 1e+20)
Above, mask=False means nothing is masked. If you wrap this with np.ma.masked_invalid , then:
In [186]: np.ma.masked_invalid(imdata) Out[186]: masked_array(data = [[3.0 5.0 10.0 -- 5.0 8.0] [3.0 5.0 10.0 -- 5.0 8.0]], mask = [[False False False True False False] [False False False True False False]], fill_value = 1e+20)
The problem with masking a is that np.vstack does not account for the mask. Alternatively, you could use np.ma.vstack . Generally speaking, only functions in the np.ma namespace are masked.
However, in fact, you do not need to use vstack here; np.atleast_2d will do. vstack creates an array of form (2, N) , and np.atleast_2d creates an array of form (1, N) .
Another alternative is to use set_over instead of set_bad . This would allow you to avoid having to mask the array:
import numpy as np import matplotlib.pyplot as plt a = np.array([3, 5, 10, np.inf, 5, 8]) imdata = np.atleast_2d(a) cmap = plt.cm.hot cmap.set_over('b') cmap.set_under('g') fig, ax = plt.subplots() b = a[np.isfinite(a)] im = ax.pcolormesh(imdata, cmap=cmap, vmin=b.min(), vmax=b.max()) plt.colorbar(im, extend='both') plt.show()

extend='both' in combination with set_over and set_under give small colored arrows in the color bar that indicate the color used for values ββthat are outside the range of the color scale.