我正在使用
plt.imshow()
在网格上绘制值(在我的例子中是 CCD 数据)。示例图:
我需要在其上指示一个障碍,以显示我关心的像素。这与我需要的类似:
我知道如何向图像添加方块,向图像添加网格线,但这些知识并不能解决问题,也不能向图片添加单个方块,这也在我的能力范围内。我需要一条线包围网格上的一个区域(这条线总是需要在像素之间,而不是穿过它们,所以这可能会让它更简单一些)。
我该怎么做?
Iury Sousa 为上述问题提供了一个很好的解决方法。然而,它并不是严格地用一条线围绕该区域(而是在图片上绘制一个蒙版,然后再次用图片覆盖其中的大部分),并且当我尝试围绕重叠的像素组时,它会失败。 ImportanceOfBeingErnest 在评论中建议我应该简单地使用
plt.plot
示例。以 Iury Sousa 的例子为起点,让我们:
X,Y = np.meshgrid(range(30),range(30))
Z = np.sin(X)+np.sin(Y)
selected1 = Z>1.5
现在
selected1
是一个布尔数组的数组,我们只想圈出那些对应Z值高于1.5的像素。我们还想圈出 selected2
,其中包含值高于 0.2 且低于 1.8 的像素的 True
值:
upperlim_selected2 = Z<1.8
selected2 = upperlim_selected2>0.2
Iury Sousa 的出色解决方法不适用于这种情况。在我看来,
plt.plot
会的。使用 selected1
或其他方法来实现 selected2
和 plt.plot
的循环的有效方法是什么?
我尝试了一些应该适合您需求的东西。
首先,我定义了一个任意数据:
X,Y = np.meshgrid(range(30),range(30))
Z = np.sin(X)+np.sin(Y)
您可以在此处定义适合您要突出显示的图案的条件:
selected = Z>1.5
要绘图,您将使用
scatter
而不是 imshow
。您将绘制所有数据,然后将所选数据绘制两次,一次使用带有突出显示颜色的较大方块,另一次通常使用相同的颜色参考和限制。
info = dict(marker='s',vmin=-2,vmax=2)
fig,ax = plt.subplots()
plt.scatter(X.ravel(),Y.ravel(),100,c=Z.ravel(),**info)
plt.scatter(X[selected].ravel(),Y[selected].ravel(),150,c='r',marker='s')
plt.scatter(X[selected].ravel(),Y[selected].ravel(),100,c=Z[selected].ravel(),**info)
ax.axis('equal')
类似于Can matplotlib轮廓匹配像素边缘?中的答案 您可以创建一个更高分辨率的网格并绘制
contour
图。
import numpy as np
import matplotlib.pyplot as plt
X,Y = np.meshgrid(range(30),range(30))
Z = np.sin(X)+np.sin(Y)
resolution = 25
f = lambda x,y: Z[int(y),int(x) ]
g = np.vectorize(f)
x = np.linspace(0,Z.shape[1], Z.shape[1]*resolution)
y = np.linspace(0,Z.shape[0], Z.shape[0]*resolution)
X2, Y2= np.meshgrid(x[:-1],y[:-1])
Z2 = g(X2,Y2)
plt.pcolormesh(X,Y, Z)
plt.contour(X2,Y2,Z2, [1.5], colors='r', linewidths=[1])
plt.show()
另一个适合我的解决方案:
让我们以网格为例:
grid=[[0, 6, 8, 2, 2, 5, 25, 24, 11],
[4, 15, 3, 22, 225, 1326, 2814, 1115, 18],
[6, 10, 9, 201, 3226, 3549, 3550, 3456, 181],
[42, 24, 46, 1104, 3551, 3551, 3551, 3382, 27],
[9, 7, 73, 2183, 3551, 3551, 3551, 3294, 83],
[9, 7, 5, 669, 3544, 3551, 3074, 1962, 18],
[10, 3545, 9, 10, 514, 625, 16, 14, 5],
[5, 6, 128, 10, 8, 6, 7, 40, 4]]
我们绘制它:
plt.pcolormesh(grid)
假设我们要圈出值大于 1420 的每个像素。我们创建一个布尔数组:
threshold=1420
booleangrid=np.asarray(grid)>threshold
intgrid=booleangrid*1
然后我们在每个像素周围创建一条线段:
down=[];up=[];left=[];right=[]
for i, eachline in enumerate(intgrid):
for j, each in enumerate(eachline):
if each==1:
down.append([[j,j+1],[i,i]])
up.append([[j,j+1],[i+1,i+1]])
left.append([[j,j],[i,i+1]])
right.append([[j+1,j+1],[i,i+1]])
并将它们连接在一起:
together=[]
for each in down: together.append(each)
for each in up: together.append(each)
for each in left: together.append(each)
for each in right: together.append(each)
(为了清晰起见,单独创建。)
我们遍历这些单独的线段,只保留那些只出现一次的线段,即由我们上面定义的布尔数组(
booleangrid
)定义的特征边缘上的线段:
filtered=[]
for each in together:
c=0
for EACH in together:
if each==EACH:
c+=1
if c==1:
filtered.append(each)
然后我们用 for 循环绘制网格和单独的线段:
plt.pcolormesh(grid)
for x in range(len(filtered)):
plt.plot(filtered[x][0],filtered[x][1],c='red', linewidth=8)
给我们结果:
我们对此感到满意。