1

I have n matrices (np.array) of floats and I want to plot them together using imshow but with each one having a different colour range for its values. e.g. n = white->blue, n+1 = white -> red etc. Is there a way of doing this?

The matrices are of the same size, and colouring over each other is not too much of an issue as the majority of the matrices' values are 0 (hope that will be white).

I was thinking of something like:

1st matrix

000
010
000

2nd matrix

000
000
001

So I thought maybe I could convert the second matrix into:

222
222
223

and then make 0->1 white to blue and 2->3 white to red.

I unfortunately have no idea how to do this with the matplotlib colormaps.

Anake
  • 7,201
  • 12
  • 45
  • 59

1 Answers1

6

imshow will not plot values that are set to None. If the data are sparse enough you can lay them on top of each other.

import numpy as np
import pylab as plt

# Your example data
A1 = np.zeros((3,3))
A2 = np.zeros((3,3))
A1[1,1] = 1
A2[2,2] = 1

# Apply a mask to filter out unused values
A1[A1==0] = None
A2[A2==0] = None

# Use different colormaps for each layer
pwargs = {'interpolation':'nearest'}
plt.imshow(A1,cmap=plt.cm.jet,**pwargs)
plt.imshow(A2,cmap=plt.cm.hsv,**pwargs)
plt.show()

enter image description here

Hooked
  • 84,485
  • 43
  • 192
  • 261