本文介绍了matplotlib-从轮廓线提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题/答案对显示了如何从等高线图中提取顶点:

This question/answer pair shows how to extract vertices from contour plot:

p = cs.collections[0].get_paths()[0]
v = p.vertices
x = v[:,0]
y = v[:,1]

但是如何获取每个路径的值(即高程模型的 z )?

But how to get the value (i.e z for an elevation model) for each path?

推荐答案

没有直接方法,但是 cs.collections cs.levels 的顺序完全相同(这是您追求的"z"值).

There's no direct way, but cs.collections is in the exact same order as cs.levels (which is the "z" values you're after).

因此,最简单的方法是:

Therefore, it's easiest to do something like:

lookup = dict(zip(cs.collections, cs.levels))
z = lookup[line_collection_artist]

作为一个快速的交互式示例:

As a quick interactive example:

import numpy as np
import matplotlib.pyplot as plt

def main():
    fig, ax = plt.subplots()
    cs = ax.contour(np.random.random((10,10)))

    callback = ContourCallback(cs)
    plt.setp(cs.collections, picker=5)
    fig.canvas.mpl_connect('pick_event', callback)

    plt.show()

class ContourCallback(object):
    def __init__(self, cs):
        self.lookup = dict(zip(cs.collections, cs.levels))

    def __call__(self, event):
        print self.lookup[event.artist]

main()

这篇关于matplotlib-从轮廓线提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 20:49