【发布时间】:2016-03-13 20:00:57
【问题描述】:
我想使用 onclick 方法在我的 matplotlib 图上选择一系列数据。但问题是,我只能这样做并更新情节。我有一些想法可以做到这一点,其中一个是制作一个图表列表,在我添加新图片后我跳转到新索引......但大多数情况下我希望能够存储来自点击的信息( event.xdata) 两次为该部分中图表下方的区域着色 - 但对于初学者来说,无论我点击哪里,都可以画点。但我觉得有比在onclick 函数中添加plt.draw() 更好的解决方案吗?
import numpy as np
import matplotlib.pyplot as plt
from itertools import islice
class ReadFile():
def __init__(self, filename):
self._filename = filename
def read_switching(self):
return np.genfromtxt(self._filename, unpack=True, usecols={0}, delimiter=',')
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
global coords
coords.append((ix, iy))
print(coords)
fig.canvas.mpl_disconnect(cid)
return coords
coords = []
filename = 'test.csv'
fig = plt.figure()
ax = fig.add_subplot(111)
values = (ReadFile(filename).read_switching())
steps = np.arange(1, len(values)+1)*2
graph_1, = ax.plot(steps, values, label='original curve')
cid = fig.canvas.mpl_connect('button_press_event', onclick)
print(coords)
graph_2, = ax.plot(coords, marker='o')
plt.show()
【问题讨论】:
标签: python python-3.x matplotlib event-handling mouseevent