Changing colours of pixels of plt.imshow() image

You've already suggested the easiest way of doing it (overlaying another image on top), but if that's not working quite how you want it to, there are other options.


Approach #1 - Manually render and composite the image


The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.

As a quick example:

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(100).reshape(10, 10)

cmap = plt.cm.gray
norm = plt.Normalize(data.min(), data.max())
rgba = cmap(norm(data))

# Set the diagonal to red...
rgba[range(10), range(10), :3] = 1, 0, 0

plt.imshow(rgba, interpolation='nearest')
plt.show()

enter image description here

A disadvantage to this method is that you can't just call fig.colorbar(im), as you're passing in a pre-rendered rgb image. Therefore, if you need a colorbar, you'll have to use a proxy artist. It's easiest to just add an additional, invisible (not drawn, rather than transparent) artist with imshow(data, visible=False) and then base the colormap on that artist. As a quick example:

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(100).reshape(10, 10)

cmap = plt.cm.gray
norm = plt.Normalize(data.min(), data.max())
rgba = cmap(norm(data))

# Set the diagonal to red
rgba[range(10), range(10), :3] = 1, 0, 0

fig, ax = plt.subplots()
ax.imshow(rgba, interpolation='nearest')

# Add the colorbar using a fake (not shown) image.
im = ax.imshow(data, visible=False, cmap=cmap)
fig.colorbar(im)

plt.show()

enter image description here

Using an invisible imshow is the easiest way to make a proxy artist for this purpose, but if speed is a concern (or if it's somehow triggering the rendering bug you mentioned) you can also use any ScalarMappable. ScalarMappable is an abstract base class that's normally only used to inherit from for colorbar support. Because we don't need to draw anything, though, we can just use it directly.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable

data = np.arange(100).reshape(10, 10)

cmap = plt.cm.gray
norm = plt.Normalize(data.min(), data.max())
rgba = cmap(norm(data))

# Set the diagonal to red
rgba[range(10), range(10), :3] = 1, 0, 0

fig, ax = plt.subplots()
ax.imshow(rgba, interpolation='nearest')

# Add the colorbar using a ScalarMappable
im = ScalarMappable(norm, cmap)
im.set_array(data)
fig.colorbar(im)

plt.show()

Approach #2 - Abuse set_bad, set_over, or set_under


The set_bad, set_over and set_under methods of the colormap to allow you to flag pixels that are NaN's or out of the specified range of the colormap.

Therefore, another way to do what you want is to set those values to NaN and specify what the NaN color should be (set_bad.. By default it's transparent for most colormaps.).

If you have an integer array or already need to have transparent NaN pixels, you can similarly abuse set_over and set_under. In this case, you'll need to manually specify the vmin or vmax when you call imshow.

As a quick example of using/abusing set_bad to do this:

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(100).reshape(10, 10).astype(float)

cmap = plt.cm.gray
cmap.set_bad((1, 0, 0, 1))

# Set the diagonal to NaN
data[range(10), range(10)] = np.nan

plt.imshow(data, cmap=cmap, interpolation='nearest')
plt.show()

enter image description here

One advantage to this method over the first one is that it's a bit easier to draw a colorbar. (The disadvantage is that this method is much less flexible.):

import numpy as np
import matplotlib.pyplot as plt

data = np.arange(100).reshape(10, 10).astype(float)

cmap = plt.cm.gray
cmap.set_bad((1, 0, 0, 1))

# Set the diagonal to NaN
data[range(10), range(10)] = np.nan

plt.imshow(data, cmap=cmap, interpolation='nearest')
plt.colorbar()
plt.show()

enter image description here