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?
At the end of you own draw() method you should call
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).