4

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

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

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

4

1 回答 1

5

没有直接的方法,但顺序与(您所追求的“z”值)cs.collections完全相同。cs.levels

因此,最简单的方法是:

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

作为一个快速交互示例:

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()
于 2013-06-12T01:50:14.847 回答