python拟合等高线_Python:从matplotlib.pyplot.contour()中查找等高线

您可以通过循环集合和路径并使用^{}的iter_segments()方法来获取顶点。

下面是一个函数,它将顶点作为一组嵌套的等高线列表、等高线截面和x、y顶点数组返回:import numpy as np

def get_contour_verts(cn):

contours = []

# for each contour line

for cc in cn.collections:

paths = []

# for each separate section of the contour line

for pp in cc.get_paths():

xy = []

# for each segment of that section

for vv in pp.iter_segments():

xy.append(vv[0])

paths.append(np.vstack(xy))

contours.append(paths)

return contours

编辑:

也可以使用未记录的matplotlib._cntrC模块计算轮廓,而无需绘制任何内容:from matplotlib import pyplot as plt

from matplotlib import _cntr as cntr

z = np.array([[0.350087, 0.0590954, 0.002165],

[0.144522, 0.885409, 0.378515],

[0.027956, 0.777996, 0.602663],

[0.138367, 0.182499, 0.460879],

[0.357434, 0.297271, 0.587715]])

x, y = np.mgrid[:z.shape[0], :z.shape[1]]

c = cntr.Cntr(x, y, z)

# trace a contour at z == 0.5

res = c.trace(0.5)

# result is a list of arrays of vertices and path codes

# (see docs for matplotlib.path.Path)

nseg = len(res) // 2

segments, codes = res[:nseg], res[nseg:]

fig, ax = plt.subplots(1, 1)

img = ax.imshow(z.T, origin='lower')

plt.colorbar(img)

ax.hold(True)

p = plt.Polygon(segments[0], fill=False, color='w')

ax.add_artist(p)

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值