How to plot only some cells with certain values of an array and others not with matplotlib.pyplot?

Anno

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.

Sheldore

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to count only certain values in one column ​and others dont touch?

Matplotlib.pyplot only displays final plot

How to plot binary values with pyplot

Matplotlib: How to plot using values stored in an array?

Recode only certain values and keep others as it is in R

Array Only Prints Certain Elements and Not Others

How to get only certain values ​from an array?

How to plot recursive function in matplotlib.pyplot?

How to convert a matplotlib.pyplot to a bokeh plot

Plot only some values of a column

How do I only plot values over a certain number in ggplot?

How to remove certain elements from an array into a new array and leave the others only the original array?

How do I remove the values of some cells in the datetime.datetime format? How do I change those values for others?

How to select only string between some certain values in Teradata SQL?

Plot certain range of values with pandas and matplotlib

How does one plot the legend with pyplot.scatter(), when the legend are only numerical values?

How to recode dataframe values to keep only those that satisfy a certain set, replace others with "other"

Add custom border to certain cells in a matplotlib / seaborn plot

How to mask some cells of a heatmap plot?

Plot only certain values in entire dataframe

How to mark cells in matplotlib.pyplot.imshow (drawing cell borders)

SQL query for returning rows with only certain values in list but not others?

Thunderbird centers some cells, but not others

Plot graph with only some values for category

Why does this *ngFor object list only have some values and not others?

In matplotlib pyplot, how to group bars in a bar plot by category?

How to plot frequency band using `matplotlib.pyplot.specgram`

How to specify scatter plot point color in matplotlib pyplot?

How to add labels and title to matplotlib.pyplot.matshow plot?