tylerthemiler - 10 months ago 81

Python Question

I am trying to overlay a matrix over another matrix using matplotlib imshow(). I want the overlay matrix to be red, and the "background matrix" to be bone, colormap wise. So, simply adding the images does not work, as you can only have one colormap. I have yet to find a different way to "overlay" besides just adding the matrices using imshow().

I am trying to, more or less, replace this matlab module.

Please let me know what alternatives I have!

Answer

Just use a masked array.

E.g.

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
x = np.arange(100).reshape(10,10)
y = np.zeros((10,10))
# Make a region in y that we're interested in...
y[4:6, 1:8] = 1
y = np.ma.masked_where(y == 0, y)
plt.imshow(x, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')
plt.show()
```

Source (Stackoverflow)