gpt4 book ai didi

python - Matplotlib 等高线图不提取精确等高线

转载 作者:行者123 更新时间:2023-12-05 04:25:04 25 4
gpt4 key购买 nike

我在提取以下数据的精确轮廓时遇到困难:(你已经可以通过查看数据看到轮廓)

data = np.array(
[[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277]])

如果我绘制它:

plt.imshow(data)

enter image description here

但是,当我尝试使用以下方法提取轮廓时:

plt.contour(data, levels = np.unique(data))

enter image description here

如您所见,轮廓并不遵循实际数据的尖角。如果我叠加两个图:

enter image description here

完整代码如下:

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277]])

plt.imshow(data)
plt.show()
plt.contour(data, levels=np.unique(data), cmap="jet")
plt.colorbar()

最佳答案

轮廓是使用 marching squares algorithm 绘制的为了计算轮廓位置,它在网格点之间进行插值。

也许您正在寻找离散区域边界:这些可以像这样检索:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

data = np.array([[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 1. , 1. ],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277],
[ 1.46184395, 1.46184395, 1.46184395, 4.24552277, 4.24552277]])


def region_borders(data, value, color, **kwargs):
v = np.argwhere(np.diff((data == value).T, axis=0))
vlines = np.array(list(zip(v + [.5, -.5], v + [.5, .5])))

h = np.argwhere(np.diff((data == value).T, axis=1))
hlines = np.array(list(zip(h + [-.5, .5], h + [.5, .5])))

if len(vlines) and len(hlines):
lines = np.vstack((vlines, hlines))
elif len(vlines):
lines = vlines
else:
lines = hlines
return mpl.collections.LineCollection(lines, colors=color, **kwargs)


contours = np.unique(data)

fig, ax = plt.subplots(ncols=len(contours)+1, sharex=True, sharey=True, layout='constrained')
im = ax[0].matshow(data, cmap='jet', aspect='auto')
fig.colorbar(im, ax=ax[-1])

norm = mpl.colors.Normalize(data.min(), data.max())
for i, value in enumerate(contours, 1):
ax[i].add_collection(region_borders(data, value, mpl.cm.jet(norm(value)), lw=2))
ax[i].set_title(value)

enter image description here

关于python - Matplotlib 等高线图不提取精确等高线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73353617/

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