【问题标题】:Get levels (contour) of a scalar field (Matplotlib / Python)获取标量场的级别(轮廓)(Matplotlib / Python)
【发布时间】:2014-09-19 22:19:53
【问题描述】:

我想知道如何获得等高线图每个级别的顶点。

例如,我有这样的情节:

我猜它有超过 10 个级别。

我正在尝试获取每个顶点。

到目前为止,我有这段代码:

def iso_contours(scalar_fields):
    for scalar_field in scalar_fields:
        cs = plt.contour(scalar_field)
        paths = cs.collections[0].get_paths()
        print len(path)
        for path in paths:
            # Get vertices

但是,我只有一条路。如何实现我想要的正确方法?

提前谢谢你。

【问题讨论】:

    标签: python matplotlib contour


    【解决方案1】:

    使用path.vertices:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x, y = np.meshgrid(np.linspace(0, 5, 100), np.linspace(0, 5, 100))
    f = x ** y
    g = y ** x
    cs = plt.contour(x, y, (f - g))
    for collection in cs.collections:
        paths = collection.get_paths()
        for path in paths:
            print(path.vertices.shape)
    
    plt.show()
    

    产量

    (7, 2)
    (28, 2)
    (51, 2)
    (172, 2)
    (154, 2)
    (51, 2)
    (28, 2)
    (7, 2)
    

    【讨论】:

    • 我的印刷品给了我 1。这是我没有得到的。 :/ 我应该把这个 [0] 参数传递给轮廓函数吗?
    • 我的错;您需要遍历 cs.collections 以收集所有路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    相关资源
    最近更新 更多