Python 如何向matplotlib中已绘制的直线添加直线?
我有一个tkinter窗口,在第一页我嵌入了matplotlib图形,还使用tkinter条目小部件获取用户值。我可以从用户值中提取,但我希望保留已绘制的内容。同时删除最近绘制的线 我是OOP新手,所以通过教程和这里的帮助,我已经走到了这一步。任何形式的建议或帮助都将不胜感激Python 如何向matplotlib中已绘制的直线添加直线?,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我有一个tkinter窗口,在第一页我嵌入了matplotlib图形,还使用tkinter条目小部件获取用户值。我可以从用户值中提取,但我希望保留已绘制的内容。同时删除最近绘制的线 我是OOP新手,所以通过教程和这里的帮助,我已经走到了这一步。任何形式的建议或帮助都将不胜感激 import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import style
import tkinter as tk
from tkinter import ttk
from tkinter import *
LARGE_FONT = ('Verdana',12)
style.use('ggplot')
from matplotlib import pyplot as plt
class Stress(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Tk.iconbitmap(self,default='icon1.ico')
tk.Tk.wm_title(self,'Stress Members')
container = tk.Frame(self)
container.pack(side='top', fill='both', expand='False')
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames ={}
frame= PageOne(container,self)
self.frames[PageOne] = frame
frame.grid(row=0, column=0, sticky='nsew')
def show_frame(self,cont):
frame=self.frames[cont]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self,text='Experiment', font = LARGE_FONT)
label.pack(pady=10,padx=10)
self.adding_widgets()
button2= ttk.Button(self,text='Validate', command=self.draw)
button2.pack()
button3= ttk.Button(self,text='Erase', command=self.dlet)
button3.pack()
self.f1= Figure(figsize=(5,5),dpi=100)
self.b=self.f1.add_subplot(1,1,1)
self.canvas = FigureCanvasTkAgg(self.f1, self)
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=True)
self.line, = self.b.plot(range(5)) ## problem here as well, want empty/blank page but have to do this to make it work
## dont know how to declare a blank empty page
def adding_widgets(self,*args):
labfram=ttk.LabelFrame(self,width=100,height=100,text='Enter Member Coordinates')
labfram.pack()
l1=ttk.Label(labfram,text='x0')
l1.pack(side='left')
self.e1=Entry(labfram,width=10,)
self.e1.pack(side='left')
l2=ttk.Label(labfram,text='y0')
l2.pack(side='left')
self.e2=Entry(labfram,width=10)
self.e2.pack(side='left')
l3=ttk.Label(labfram,text='x1')
l3.pack(side='left')
self.e3=Entry(labfram,width=10)
self.e3.pack(side='left')
l4=ttk.Label(labfram,text='y1')
l4.pack(side='left')
self.e4=Entry(labfram,width=10)
self.e4.pack(side='left')
def draw(self):
print("x0 %s\ny0 %s\nx1 %s\ny1 %s" % (self.e1.get(), self.e2.get(),self.e3.get(),self.e4.get()))
self.line.set_xdata([float(self.e1.get()), float(self.e3.get())])
self.line.set_ydata([float(self.e2.get()),float(self.e4.get())])
self.canvas.draw()
## Above code draws a line but I want to keep it so user can draw one line after the other
def dlet(self):
# want to delete the most recent line drawn here
return None
app = Stress()
app.mainloop()
通过研究和测试,我能够解决ike现在能够画一条接一条的线,以及如何删除最近的线
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import style
import tkinter as tk
from tkinter import ttk
from tkinter import *
LARGE_FONT = ('Verdana',12)
style.use('ggplot')
from matplotlib import pyplot as plt
class Stress(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Tk.iconbitmap(self,default='icon1.ico')
tk.Tk.wm_title(self,'Stress Members')
container = tk.Frame(self)
container.pack(side='top', fill='both', expand='False')
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames ={}
frame= PageOne(container,self)
self.frames[PageOne] = frame
frame.grid(row=0, column=0, sticky='nsew')
def show_frame(self,cont):
frame=self.frames[cont]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self,text='Experiment', font = LARGE_FONT)
label.pack(pady=10,padx=10)
self.adding_widgets()
button2= ttk.Button(self,text='Validate', command=self.draw)
button2.pack()
button3= ttk.Button(self,text='Erase', command=self.dlet)
button3.pack()
self.lines_pts=[] ## list to add data points
self.f1= Figure(figsize=(5,5),dpi=100)
self.b=self.f1.add_subplot(1,1,1)
self.b.set_xlim([0,10]) ## x axis limits
self.b.set_ylim([0,10]) ## y axis limits
self.canvas = FigureCanvasTkAgg(self.f1, self)
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=True)
self.lines = [] ## an emplt list to append plots
def adding_widgets(self,*args):
labfram=ttk.LabelFrame(self,width=100,height=100,text='Enter Member Coordinates')
labfram.pack()
l1=ttk.Label(labfram,text='x0')
l1.pack(side='left')
self.x0=Entry(labfram,width=10,)
self.x0.pack(side='left')
l2=ttk.Label(labfram,text='y0')
l2.pack(side='left')
self.y0=Entry(labfram,width=10)
self.y0.pack(side='left')
l3=ttk.Label(labfram,text='x1')
l3.pack(side='left')
self.x1=Entry(labfram,width=10)
self.x1.pack(side='left')
l4=ttk.Label(labfram,text='y1')
l4.pack(side='left')
self.y1=Entry(labfram,width=10)
self.y1.pack(side='left')
def draw(self):
p0 = float(self.x0.get()), float(self.y0.get())
p1 = float(self.x1.get()), float(self.y1.get())
self.lines_pts.append((p0,p1))
for p0,p1 in self.lines_pts:
x0, y0, x1, y1 = *p0, *p1
X = x0,x1
Y = y0,y1
self.lines.append(self.b.plot(X,Y)) ## this plots the line and appends in a list of lines
self.canvas.draw()
print(self.b.lines) # gives line object and is a matplotlib thing
def dlet(self):
self.b.lines.remove(self.b.lines[-1])
self.canvas.draw()
app = Stress()
app.mainloop()