gpt4 book ai didi

python - 在Python中绘制选定网格区域外边缘的方法

转载 作者:行者123 更新时间:2023-12-03 17:11:05 24 4
gpt4 key购买 nike

我有以下代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi/2, np.pi/2, 30)
y = np.linspace(-np.pi/2, np.pi/2, 30)
x,y = np.meshgrid(x,y)

z = np.sin(x**2+y**2)[:-1,:-1]

fig,ax = plt.subplots()
ax.pcolormesh(x,y,z)
这给出了这个图像:
enter image description here
现在假设我想突出显示某些网格框的边缘:
highlight = (z > 0.9)
我可以使用轮廓函数,但这会导致“平滑”轮廓。我只想在网格框的边缘之后突出显示区域的边缘。
我最接近的是添加这样的东西:
highlight = np.ma.masked_less(highlight, 1)

ax.pcolormesh(x, y, highlight, facecolor = 'None', edgecolors = 'w')
这给出了这个情节:
enter image description here
这很接近,但我真正想要的是只突出显示“ donut ”的外边缘和内边缘。
所以本质上我正在寻找一些轮廓和 pcolormesh 函数的混合 - 遵循某个值的轮廓,但在“步骤”中遵循网格箱而不是连接点对点。那有意义吗?
旁注:在 pcolormesh 参数中,我有 edgecolors = 'w' ,但边缘仍然是蓝色的。那里发生了什么事?
编辑:
JohanC 使用 add_iso_line() 的初始答案适用于提出的问题。但是,我使用的实际数据是一个非常不规则的 x,y 网格,无法转换为 1D(如 add_iso_line() 所要求的那样)。
我正在使用已从极坐标(rho,phi)转换为笛卡尔(x,y)的数据。 JohanC 提出的 2D 解决方案似乎不适用于以下情况:
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage

def pol2cart(rho, phi):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(x, y)

phi = np.linspace(0,2*np.pi,30)
rho = np.linspace(0,2,30)

pp, rr = np.meshgrid(phi,rho)

xx,yy = pol2cart(rr, pp)

z = np.sin(xx**2 + yy**2)

scale = 5
zz = ndimage.zoom(z, scale, order=0)

fig,ax = plt.subplots()
ax.pcolormesh(xx,yy,z[:-1, :-1])

xlim = ax.get_xlim()
ylim = ax.get_ylim()
xmin, xmax = xx.min(), xx.max()
ymin, ymax = yy.min(), yy.max()
ax.contour(np.linspace(xmin,xmax, zz.shape[1]) + (xmax-xmin)/z.shape[1]/2,
np.linspace(ymin,ymax, zz.shape[0]) + (ymax-ymin)/z.shape[0]/2,
np.where(zz < 0.9, 0, 1), levels=[0.5], colors='red')
ax.set_xlim(*xlim)
ax.set_ylim(*ylim)
enter image description here

最佳答案

This post显示了一种绘制此类线条的方法。因为要适应当前的pcolormesh 并不简单。 ,下面的代码演示了一种可能的适应方式。
请注意,x 和 y 的 2d 版本已重命名,因为线段需要 1d 版本。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

x = np.linspace(-np.pi / 2, np.pi / 2, 30)
y = np.linspace(-np.pi / 2, np.pi / 2, 30)
xx, yy = np.meshgrid(x, y)

z = np.sin(xx ** 2 + yy ** 2)[:-1, :-1]

fig, ax = plt.subplots()
ax.pcolormesh(x, y, z)

def add_iso_line(ax, value, color):
v = np.diff(z > value, axis=1)
h = np.diff(z > value, axis=0)

l = np.argwhere(v.T)
vlines = np.array(list(zip(np.stack((x[l[:, 0] + 1], y[l[:, 1]])).T,
np.stack((x[l[:, 0] + 1], y[l[:, 1] + 1])).T)))
l = np.argwhere(h.T)
hlines = np.array(list(zip(np.stack((x[l[:, 0]], y[l[:, 1] + 1])).T,
np.stack((x[l[:, 0] + 1], y[l[:, 1] + 1])).T)))
lines = np.vstack((vlines, hlines))
ax.add_collection(LineCollection(lines, lw=1, colors=color))

add_iso_line(ax, 0.9, 'r')
plt.show()
resulting plot
这是对第二个答案的改编,它只能用于二维数组:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from scipy import ndimage

x = np.linspace(-np.pi / 2, np.pi / 2, 30)
y = np.linspace(-np.pi / 2, np.pi / 2, 30)
x, y = np.meshgrid(x, y)

z = np.sin(x ** 2 + y ** 2)

scale = 5
zz = ndimage.zoom(z, scale, order=0)

fig, ax = plt.subplots()
ax.pcolormesh(x, y, z[:-1, :-1] )
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
ax.contour(np.linspace(xmin,xmax, zz.shape[1]) + (xmax-xmin)/z.shape[1]/2,
np.linspace(ymin,ymax, zz.shape[0]) + (ymax-ymin)/z.shape[0]/2,
np.where(zz < 0.9, 0, 1), levels=[0.5], colors='red')
ax.set_xlim(*xlim)
ax.set_ylim(*ylim)
plt.show()
second example

关于python - 在Python中绘制选定网格区域外边缘的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63458863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com