【发布时间】:2021-05-04 20:20:04
【问题描述】:
首先我要感谢一位用户@j_4321,他在这个问题上帮助了我很多How to plot an automatic graph using mouse without clicking MATPLOTLIB。
我很理解你的方法,但是我想用函数来做这个方法只是为了熟悉python。
但是使用这种方法,每次移动鼠标时,都会弹出一个新图形,我想做的是在同一个图形上重新绘制。
这是我的代码:
import tkinter as tk
from tkinter import messagebox, filedialog
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import numpy as np
import os
from readgssi import readgssi
# data
Data = []
xData = []
xAxes = []
def readDZT():
global xAxes, Data
file_path = filedialog.askopenfilename()
file_name, file_extension = os.path.splitext(file_path)
if file_extension == '.DZT':
messagebox.showinfo("INFO", "Your DZT File Has Been Selected Successfully")
hdr, arrs, gps = readgssi.readgssi(infile=file_path, verbose=True)
Samples = hdr['rh_nsamp']
X_Axes = np.array(range(0, Samples))
xAxes = X_Axes[2::1]
df = pd.DataFrame(arrs[0])
Data = df.iloc[2::1, 0::1]
fig1 = plt.figure()
# clear plots
plt.clf()
# plot 2D map
plt.imshow(Data, aspect='auto', cmap='bone')
plt.connect('motion_notify_event', mouse_move)
fig1.canvas.draw_idle()
plt.show()
else:
messagebox.showwarning("WARNING", "You Have Been Selected a Different Format")
def mouse_move(event):
x = event.xdata
print(x)
if len(Data) and x is not None: # there is something to plot
fig2 = plt.figure()
plt.clf()
x = int(x)
plt.plot(xAxes, Data[x])
fig2.canvas.draw_idle()
plt.show()
root = tk.Tk()
root.title("IHM")
root.geometry("1000x800")
Resources_frame = tk.LabelFrame(root, bd=2, relief=tk.GROOVE, text="Conversion Area")
Resources_frame.place(x=5, y=5, width=250, height=80)
tk.Label(Resources_frame, text="Select your File ").grid(row=0, column=0, sticky=tk.W)
tk.Label(Resources_frame, text="Convert Selected file ").grid(row=1, column=0, sticky=tk.W)
btn_rs = tk.Button(Resources_frame, relief=tk.GROOVE, padx=8, pady=1, text="Browse",
command=readDZT).grid(row=0, column=1)
root.mainloop()
你有什么建议吗?
【问题讨论】:
标签: python matplotlib mouseevent