views:

63

answers:

2

I'm having a problem with matplotlib where it only plots a line the first time i run the draw() function I made for it. My code is like this:

I have graphPanel.py: (stripped to the essentials)

class GraphPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.figure = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.figure)

        self.draw()

    def draw(self, curves={}):       #This creates the axis and
        if not hasattr(self, "ax"):  #plots all the lines in the curve dictionary
            self.ax = self.figure.add_subplot(111)

        self.ax.lines = []

        for name, props in zip(curves.keys(), curves.values()):
            x, y, col = props
            line = self.ax.plot(x, y, color=col, lw=3)

And the Main.py file which contains the entire interface where the graphpanel is embedded, along with a function that should call the panel's draw() function, giving it a dictionary containing a set of curves. The problem is, when I do this it will completely ignore the list self.ax.lines being emptied and the plot() call and leave everything as it was before.

For some reason it only works the first time i call it, from the init of the class, because if i put a dictionary with points in the draw() call placed in init it will plot them perfectly.

Why doesn't it work if i call draw() another time?

A: 

For people with my same problem

I solved it with a little "hack"

I made a function in the main window that destroys the graphpanel, deletes its value (del graphpanel) and then creates a new one, sending it a dictionary with the curve data too.

Then I "shake" the windows (I've made another function that increases its size by 1px and then takes it back to normal, to force an evt_size event) to make the graphpanel resize (or else it would be a tiny square at the top of the window.

Gabriele Cirulli
+1  A: 

At the end of you own draw() method you should call

self.canvas.draw()

to tell the canvas it has to update the screen right now. Otherwise it will do so on the next draw event (which you cause by that terrible hack you mentioned).

Ber