Correct usage of Mpl backend...

Hello,
I' m new to MPL...and rather new to Python...
so I hope I wont say too much stupidities...

I' m writing a little Fileviewer...that will plot the data of the
file...depending on some user inputs (not implemented yet)

so I started playing around...using the Tk embbeded plot exemple...
I added a "Plot" button in particular...that will plot the figure each time
it is pressed...

The program is working as expected...except memory wise...the program keeps
growing...
each time plot is pressed it's growing...I believe it's due to some poor
programming on my side, but I can't really put my finger on it right now. I
suspect that I have 2 competing mainloop maybe but i' m not really sure

If you guys could help me...
here's the source:

from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
FigureCanvasAgg
from matplotlib.figure import Figure

class SegDviewer():
    """class for the SegD viewer"""
    
    def __init__(self):
        """window constructor"""
        self.already_plotted = False
        self.iter = 0
        
        self.root = Tk()
        self.root.title("Viewer a la con")
        self.draw_button()
        self.draw_frame()
        self.root.mainloop()
        
    def draw_button(self):
        """places the buttons on the window"""
        Button(self.root, text = "Open" , width = 9, command =
self.openchoose ).pack( side = TOP )
        Button(self.root, text = "Plot" , width = 9, command =
self.showplot ).pack()
        Button(self.root, text = "Quit" , width = 9, command =
self.root.quit ).pack( side = BOTTOM)
        
    def openchoose(self):
        """open a file chooser"""
        import tkFileDialog
        
        return tkFileDialog.askopenfilename()
        
    def draw_frame(self):
        """ """
        self.myFrame = Frame(self.root, bg = 'Dark grey', height = 4*100,
width = 10*100)
        self.myFrame.pack( side = RIGHT )
        
    def showplot(self):
        """ """
        
        try:

···

#
            self.mycanvas._tkcanvas.destroy()
        # #self.myCanvas.delete(ALL)
        # #self.mycanvas.destroy()
        except:
            print "there was nothing to destroy"
            
        if (True):
            #self.myCanvas = Canvas(self.myFrame, bg = 'Dark grey', height =
4*100, width = 10*100)
            self.f = Figure(figsize=(10,4), dpi=100)

            a = self.f.add_subplot(111)
            t = arange(0.0,5.0,0.1)
            
            s = sin(self.iter*pi*t)
            
            a.plot(t,s)
            #a.set_title('channel')
            #a.set_xlabel('X axis label')
            #a.set_ylabel('Y label')
            self.mycanvas = FigureCanvasTkAgg(self.f, master = self.myFrame)
            self.mycanvas.draw()
            #self.mycanvas.get_tk_widget().pack(side=TOP)
            self.mycanvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
            # a tk.DrawingArea
            
            #self.myCanvas.pack()
            #self.mycanvas.draw()
            
            self.iter = self.iter + 1

application = SegDviewer()


View this message in context: http://www.nabble.com/Correct-usage-of-Mpl-backend…-tp21012938p21012938.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

By default matplotlib overplots, so every time you call plot a new
line is added to the canvas. You can turn this behavior off using the
hold method

  ax.hold(False)

or setting the axes.hold rc parameter -- see
http://matplotlib.sourceforge.net/users/customizing.html

JDH

···

On Mon, Dec 15, 2008 at 6:20 AM, vehemental <jimmy.paillet@...287...> wrote:

The program is working as expected...except memory wise...the program keeps
growing...
each time plot is pressed it's growing...I believe it's due to some poor
programming on my side, but I can't really put my finger on it right now. I
suspect that I have 2 competing mainloop maybe but i' m not really sure

Sorry, on second look it appears you have a more serious problem.
Typically you want to create your figure and canvas just once and add
it to your GUI. And then when the user clicks plot, just draw to the
existing axes

def __init__():
    self.figure = some code
    self.canvas = some code
    self.axes = some code

def onclick(self):
    self.axes.cla() # clear if you want
    self.axes.plot(something)

Ie, you want to reuse the same figure/canvas/axes instead of
recreating them all the time

···

On Mon, Dec 15, 2008 at 9:44 AM, John Hunter <jdh2358@...287...> wrote:

By default matplotlib overplots, so every time you call plot a new
line is added to the canvas. You can turn this behavior off using the
hold method

Thanks.
I understood the explanation you gave me...was not so sure how it translated
in the language...
so what i did is creating a init_plot method...that is creating a figure
once and for all (empty) , attaching it to a canvas and pack it...

then I have my showplot routine activated by the button with contains only
the formula for X,Y, the plot statement and the show()
statement...and it worked!

now the plot window is correctly updated and ram usage is stable.
Thanks for the help!
I willl dig deepert o understand this better...

···


View this message in context: http://www.nabble.com/Correct-usage-of-Mpl-backend…-tp21012938p21020254.html
Sent from the matplotlib - users mailing list archive at Nabble.com.