Ivy Gao
Ivy Gao

Reputation: 111

Combine several heat maps

I have several matrix, which i would like to show them with different colours, but in one heat map figure. For example:

import numpy as np
a=np.array([[0,0,0],[0,0,0],[1,0,0]])
b=np.array([[0,0,0],[2,2,0],[0,0,0]])
c=np.array([[3,0,0],[0,3,0],[0,0,3]])

a+b+c=np.array([[3, 0, 0],
   [2, 5, 0],
   [1, 0, 3]])

. I would like to give the non-zero position different color based on 1,2,3 in one heat map figure. There is no problem for a and b, but for b and c there overlap. So how can i show them clearly? So for the final matrix a+b+c, i hope people can understand 1 happening at 3,1 position with one color, 2 happening at 2,1 and 2,2 with another color and 3 happening at position 1,1 and 2,2 and 3,3 with the third color.

Upvotes: 0

Views: 622

Answers (1)

bastien girschig
bastien girschig

Reputation: 855

I'm still a little bit confused about what you are trying to achieve, but I hope this is what you're looking for

I've assigned each array to a channel in the output image (red, green and blue) This gives the following color map:

  • no values: black
  • a, b, and c have values: white
  • only a: red
  • only b: green
  • a and b have values: yellow
  • etc...

here is the snippet:

import numpy as np
from matplotlib import pyplot as plt

# input data
a = np.array([[0,0,0],[0,0,0],[1,0,0]])
b = np.array([[0,0,0],[2,2,0],[0,0,0]])
c = np.array([[3,0,0],[0,3,0],[0,0,3]])

# transform the data so that all values are either 0 or one
a = a.astype(bool)
b = b.astype(bool)
c = c.astype(bool)

# create the empty output image (heatmap)
width, height = a.shape
img = np.zeros((width, height, 3))
# put the data into the image
img[:,:,0] = a
img[:,:,1] = b
img[:,:,2] = c

plt.imshow(img)
plt.show()

And here is the result: heatmap

Hope it helps

Upvotes: 1

Related Questions