简体   繁体   English

如何只绘制某些具有某些数组值的单元格,而另一些则不使用matplotlib.pyplot绘制?

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

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). Ì尝试了以下代码,我也将y的alpha更改为1。问题是,要么y的红色窗口从x更改(y的alpha = 0.5),要么甚至看不到x的图(y的alpha = 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? 如何只绘制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. 结果应为:x作为背景,并且每个位置(其中y为1)都应上纯红色。

Following the approach in this answer linked by @ImportanceOfBeingEarnest, the exact solution in your case would look like below. 按照@ImportanceOfBeingEarnest链接的答案中的方法,您的情况的确切解决方案如下所示。 Here, np.ma.masked_where will mask your y array at places where it is 0. The resulting array will only contain 1. 在这里, np.ma.masked_where将在y数组为0的地方屏蔽它。结果数组仅包含1。

EDIT: The problem of overlaying seems to stem from the choice of cmap . 编辑:覆盖的问题似乎源于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 . 如果不为y指定cmap ,则可以清楚地看到,在下面确实确实只绘制了1并将其覆盖在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()

在此处输入图片说明

We can inverse "The result should be [..] x as background and every position, where y is 1, should be colored pure red." 我们可以反过来说: “结果应为[..] x作为背景,并且每个位置(其中y为1)应被上色为纯红色。” , namely to just plot x , masked by y and set the background to red. ,即仅绘制x并用y遮盖,然后将背景设置为红色。

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()

在此处输入图片说明

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? Matplotlib imshow:如何在矩阵上应用遮罩,或者如何在matplotlib中使用imshow将NaN值绘制为特殊颜色?

and there is also an example on the matplotlib page: Image masked matplotlib页面上还有一个示例: 图像被遮罩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM