How to map integers to colors in matplotlib?
<p><strong>Update:</strong>
Did a lousy hack where I choose a colormap gradient ('nipy_spectral' in my case) and make a dictionary that maps the color I want to an integer such that the plotting function will plot the correct color.  It involves a lot of futzing around with the numbers.  I'll post as solution if nothing better comes along.</p>
<hr />
<p>I have the following plotting function adapted from another plotting function I used for a confusion matrix:</p>
<pre><code>def plot_matrix(rm, title='Robot World', cmap=plt.cm.Blues):
    plt.imshow(rm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.tight_layout()
</code></pre>
<p>And I have a matrix of the following form:</p>
<blockquote>
<p>[ 1,3,1,0,1,3,4]</p>
<p>[ 3,1,0,1,2,3,0]</p>
<p>... for a few more rows.</p>
</blockquote>
<p>Ideally, I plot this matrix such that the integer values within the matrix correspond to the colors: <code>['black','blue','yellow','green','red']</code> in a plot that looks something like the example matrix on matplotlib's example site for interpolation nearest:</p>
<p><a href="https://i.stack.imgur.com/2MGgv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2MGgv.png" alt="enter image description here" /></a></p>
<hr />
<p>I think the answer must be some kind of custom cmap, like:</p>
<pre><code>cmap = { 0:'k',1:'b',2:'y',3:'g',4:'r' }
</code></pre>
<p>How would I do this in matplotlib?  I am looking at the custom colormap site, and it seems a whole lot more involved than what I am trying to do...</p>