【发布时间】:2020-04-17 01:37:15
【问题描述】:
我想根据鼠标点击的坐标编辑在我的图表上绘制的坐标。具体来说,我希望我点击的点消失。现在,我的代码在两个图表之间切换,在第二个图表上我想删除最接近点击的点,然后显示新图表。
我想我需要这样的东西?我还希望能够保存新数组,删除点。
* EDIT * 我已经让它工作了,所以当我点击它时会删除点,但是,我想删除多个点并保存删除点的数组.
def on_button_press(self, event):
print('xdata, ydata:', event.xdata, event.ydata)
#find the x index closest to the mouse click
array = np.asarray((points_x))
idx = (np.abs(array - event.xdata)).argmin()
#remove those points from points_x and points_y
points_x_adjust = np.delete(points_x, idx)
points_y_adjust = np.delete(points_y, idx)
#replot
self.canvas.flush_events()
self.ax.clear()
self.ax.plot(Z_filt_2)
self.ax.set(title='Points removed')
# self.ax.set(xlim=(touchdown_cut_adj[-11]-500,toeoff_cut_adj[-1]+500))
# self.ax.plot(toeoff_cut_adj,plt_TOy_adj, marker='o', linestyle='none')
# x,y = zip(*new_points)
self.ax.plot(points_x_adjust,points_y_adjust, marker='x', linestyle='none')
self.canvas.draw()
在这里它被集成到我的整个代码中
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
# (not so) random data
# (not so) random data
Z_filt_1 = [0,1,2,3,4,5,6,7,8,9,10]
Z_filt_2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
points_x = [2,4,6,8,10]
points_y = [2,4,6,8,10]
class MatPlotLibSwitchGraphs:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.frame.pack(expand=YES, fill=BOTH)
self.fig = Figure(figsize=(5, 4), dpi=100)
self.ax = self.fig.gca() #config_plot()
self.canvas = FigureCanvasTkAgg(self.fig, self.master)
self.config_window()
self.graphIndex = 0
self.draw_graph_one()
def config_window(self):
toolbar = NavigationToolbar2Tk(self.canvas, self.master)
toolbar.update()
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
print('connect')
self.canvas.mpl_connect("key_press_event", self.on_key_press)
self.canvas.mpl_connect("button_press_event", self.on_button_press)
self.button = Button(self.master, text="QUIT", command=self._quit)
self.button.pack(side=BOTTOM)
self.button_switch = Button(self.master, text="CHANGE PLOT", command=self.switch_graphs)
self.button_switch.pack(side=BOTTOM)
def draw_graph_one(self):
self.ax.clear()
self.ax.plot(Z_filt_1)
self.ax.plot(points_x,points_y, marker='x', linestyle='none')
self.ax.set(title='First Plot')
self.canvas.draw()
def draw_graph_two(self):
self.ax.clear()
self.ax.plot(Z_filt_2)
self.ax.plot(points_x,points_y, marker='x', linestyle='none')
self.ax.set(title='Second Plot')
self.canvas.draw()
def on_key_press(self, event):
print('key:', event.key)
def on_button_press(self, event):
print('xdata, ydata:', event.xdata, event.ydata)
#find the x index closest to the mouse click
array = np.asarray((points_x))
idx = (np.abs(array - event.xdata)).argmin()
#remove those points from points_x and points_y
points_x_adjust = np.delete(points_x, idx)
points_y_adjust = np.delete(points_y, idx)
#replot
self.canvas.flush_events()
self.ax.clear()
self.ax.plot(Z_filt_2)
self.ax.set(title='Points removed')
# self.ax.set(xlim=(touchdown_cut_adj[-11]-500,toeoff_cut_adj[-1]+500))
# self.ax.plot(toeoff_cut_adj,plt_TOy_adj, marker='o', linestyle='none')
# x,y = zip(*new_points)
self.ax.plot(points_x_adjust,points_y_adjust, marker='x', linestyle='none')
self.canvas.draw()
def _quit(self):
#self.master.quit() # stops mainloop
self.master.destroy() # works better then `quit()` (at least on Linux)
def switch_graphs(self):
# Need to call the correct draw, whether we're on graph one or two
self.graphIndex = (self.graphIndex + 1 ) % 2
if self.graphIndex == 0:
self.draw_graph_one()
else:
self.draw_graph_two()
def main():
root = Tk()
MatPlotLibSwitchGraphs(root)
root.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
标签: python class matplotlib tkinter user-input