Matplotlib animation preventing wxpython frame close

0

well i have a wxpython frame with a wxNotebook with different tabs holding a wxpanel with a figure embedded. This wxpanel is a whole class that do real-time plot from a server. There's now like 7 figures doing real time plot. Well, the thing is that when i have the wxpanel in the GUI and is plotting the mainframe do not close.

I haves tried different things. First i removed all the panels. The app close very smoothly. Second i added only 3 of the plot corresponding to one of the instruments, now it close, sometimes smoothly others don't and throwing this kinda error

Process finished with exit code -1073741819 (0xC0000005)

But when i have all of the on. It does not close. This is the code of my embedded panel. Im just to fix this problem because its getting very frustrating not being able to close the frame.

class plot_panel(wx.Panel):
    def __init__(self, parent, model, x, y, title):
        wx.Panel.__init__(self, parent)
        sns.set()

        self.title = title
        self.line_width = 1
        self.figure = plt.figure(figsize=(18, 3))
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axis = self.figure.add_subplot(1, 1, 1)
        self.toolbar = NavigationToolbar(self.canvas)
        self.toolbar.Realize()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 0, wx.EXPAND)
        sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.SetSizerAndFit(sizer)

        self.x_axis = []
        self.y_axis = []

        self.Bind(wx.EVT_CLOSE, self.stop)

        self.ani = animation.FuncAnimation(self.figure, self.animate, fargs=(self.x_axis, self.y_axis, model, x, y), interval=500)
        self.canvas.draw()

    def stop(self):
        self.ani.event_source.stop()


    def animate(self, i, x_axis, y_axis, model, x, y):
        with session_scope() as s:
            value = s.query(y).order_by(model.Datetime.desc()).first()
            time = s.query(x).order_by(model.Datetime.desc()).first()
            if type(time) != type(None) and type(value) != type(None):
                time = time[0]
                value = round(value[0], 2)

                x_axis.append(time)
                y_axis.append(value)

        x_axis = x_axis[-1800:]
        y_axis = y_axis[-1800:]

        self.axis.clear()
        self.axis.set_title(self.title)
        self.axis.set_xlim(min(x_axis), max(x_axis) + timedelta(seconds=0.01))
        self.axis.set_xlabel(x.name)
        self.axis.set_ylabel(y.name)
        self.axis.plot(x_axis, y_axis, linewidth=self.line_width)
        self.axis.annotate(str(value), xy=(time, value), xytext=(10, -2), textcoords='offset pixels',
                           bbox=dict(boxstyle=custom_box_style, alpha=0.2))

        # Format plot
        plt.subplots_adjust(left=0.2, right=0.85, top=0.92, bottom=0.2, wspace=1) 

I think its something related to the animation and the close event.

Edit: I added a button to stop the animation via self.ani.event_source.stop(), i added al the plots that i have right know and tried, still not closing, then i started stopping plot animations, the when i reach 3 animations stopped the app closes. So' the event close is block for some reason, maybe to many figures animating?

Edit 2: Tried something new, runned the scrip. Tried to close, nothing happens, then i stop only one of the animations, it closed. Ran again the scrip, stopped only one animation and then tried closing, now it closes. But if one animation if not stopped it would not close.

Edit 3: Well, i ran the script to see any other behavior. This time i dint press close right away. So' what i did was to stop one animation, go to another tab or plot an try and close from there, dint close, maybe the animation from the tab. Because when i went back to the stopped animation i dint close, but when i hit the close button, it closed.

python
python-2.7
matplotlib
animation
wxpython
asked on Stack Overflow Oct 15, 2019 by Lemanuel Colon • edited Oct 15, 2019 by Lemanuel Colon

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0