This question/answer pair显示了如何从等高线图中提取顶点:
p = cs.collections[0].get_paths()[0]
v = p.vertices
x = v[:,0]
y = v[:,1]
但是如何获得每条路径的值(即z
高程模型)?
答案 0 :(得分:2)
没有直接的方式,但cs.collections
与cs.levels
的顺序完全相同(这是您所追求的“z”值)。
因此,最简单的做法是:
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()