2

I have an image that consists of float values and another one that consist only of ones and zeros. I want to plot the second image over the first one, but I only want to plot the ones from the second image. The zeros shall not be plotted.

Ì have tried the following code and I also changed the alpha of y to 1. The problem is, that either the red windows of y are changed from x (alpha of y = 0.5), or one can not even see the plots of x (alpha of y=1).

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random(size=(20,20))
y = np.random.randint(2, size=(20,20))

fig = plt.figure()
plt.imshow(x, cmap="Greys", alpha = 0.5)
plt.imshow(y, cmap="Reds", alpha = 0.5)
plt.show()

How can I only plot the ones of y?

UPDATE: Thank you for your answers! But this is not want I am looking for. I will explain again:

The result should be something like: x as background and every position, where y is 1, should be colored pure red.

Anno
  • 761
  • 1
  • 10
  • 22
  • your question is more about filtering/masking arrays than plotting, right? – Paul H Jun 07 '19 at 14:16
  • Inprinciple the answer is given [here](https://stackoverflow.com/questions/32991649/matplotlib-imshow-how-to-apply-a-mask-on-the-matrix). Alternatively, [this](https://stackoverflow.com/questions/2578752/how-can-i-plot-nan-values-as-a-special-color-with-imshow-in-matplotlib) would be a way, to do it, setting the bad value to a color with alpha=0, like (0,0,0,0). – ImportanceOfBeingErnest Jun 07 '19 at 14:16

2 Answers2

0

Following the approach in this answer linked by @ImportanceOfBeingEarnest, the exact solution in your case would look like below. Here, np.ma.masked_where will mask your y array at places where it is 0. The resulting array will only contain 1.

EDIT: The problem of overlaying seems to stem from the choice of cmap. If you don't specify the cmap for the y, you can clearly see below that indeed only 1's are plotted and overlaid on the top of x. In order to have a discrete color (red in your case), you can create a custom color map

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors

x = np.random.random(size=(20,20))
y = np.random.randint(2, size=(20,20))

y_new =np.ma.masked_where(y==0, y)
cmap = colors.ListedColormap(['red'])

fig = plt.figure()
plt.imshow(x, cmap="Greys", alpha = 0.5)
plt.imshow(y_new, cmap=cmap, alpha=1)
plt.show()

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thank you very much, but please see my update. This solution still mixes the two plots somehow... They should look like y is layed over x, but only the ones in y are plotted. – Anno Jun 07 '19 at 14:32
0

We can inverse "The result should be [..] x as background and every position, where y is 1, should be colored pure red.", namely to just plot x, masked by y and set the background to red.

import matplotlib.pyplot as plt
import numpy as np

y = np.random.randint(2, size=(20,20))
x = np.random.random(size=(20,20))
X = np.ma.array(x, mask=y)

fig = plt.figure()

plt.imshow(X, cmap="Greys")
plt.gca().set_facecolor("red")
plt.show()

enter image description here

There are of course related Q&As like

Matplotlib imshow: how to apply a mask on the matrix or How can I plot NaN values as a special color with imshow in matplotlib?

and there is also an example on the matplotlib page: Image masked

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712