Assertion triggered in wxMemory DC

Thank you for the help

I removed the show() command from the loop. Now just calling grid(True)
multiple times. On my machine I narrowed the loop to 57 and it started
happening. At 50 it did not.

Another way I found to trigger this problem is to remove the for loop all
together. Then it just pops up the display. Now grab the window with the
mouse and shake the window continuously around, forcing multiple Paint
events. Eventually it triggers the assert. The application I am developing
using multiple matplotlib windows in an MDI window seems to trigger this
quite easily. Everytime a window is added to the MDIParent all MDIChild
windows gets repainted. Something to do with the paint event.

By the way Kudos for matplotlib. Amazing. I had to dig around the code and
news groups to figure out how to find the user coordinates of a mouse click.
Reading the code opens your eyes the wonderful design that is backend
dependent.

import wx
import matplotlib
import time
matplotlib.use('WX')

from matplotlib.matlab import *

class App(wx.App):
    """Application class."""

    def OnInit(self):
        t = arange(0.0, 2.0, 0.01)
        s = sin(2*pi*t)
        l = plot(t, s, linewidth=1.0)
        xlabel('time (s)')
        ylabel('voltage (mV)')
        title('About as simple as it gets, folks')
        legend('line', loc='upper right')
        
        for i in range(100):
            grid(True)
        return True

···

#---------------------------------------------------------------------------
# run the app
app = App()
app.MainLoop()

-----Original Message-----
From: John Hunter [mailto:jdhunter@…4…]
Sent: Monday, June 28, 2004 11:17 PM
To: Schalkwyk, Johan
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Assertion triggered in wxMemory DC

"Schalkwyk," == Schalkwyk, Johan <Johan.Schalkwyk@...227...>

writes:

    Schalkwyk,> The code snippet below reproduces the
    Schalkwyk,> problem. Basically calling "show()" in a loop forces
    Schalkwyk,> repaint of the same window many times. After a while a
    Schalkwyk,> strange stack trace appears with the assertion
    Schalkwyk,> above. Sometimes the stack trace creates garbage all
    Schalkwyk,> over the screen which has to be cleared by repainting
    Schalkwyk,> the whole screen.

You should only call show at most once per matplotlib script - for
more information on show see http://matplotlib.sf.net/faq.html#SHOW.
When embedding matplotlib in an application, typically you won't use
show at all.

When embedding matplotlib in a GUI like WX, you should use
canvas.draw() to repaint the figure. See embedding_in_wx.py in the
examples directory of the matplotlib src distribution of the script
embedding_in_wx2.py attached with this email for an example of how to
use matplotlib in a wx app.

I can't promise you this will fix your problem, but will at least make
your example consistent with how matplotlib is meant to be used in a
wx app. If you still get a problem, please post the revised code and
I'll take a look.

Good luck!
JDH

#!/usr/bin/env python
"""
An example of how to use wx or wxagg in an application w/o the toolbar
"""

from matplotlib.numerix import arange, sin, pi

import matplotlib

# uncomment the following to use wx rather than wxagg
#matplotlib.use('WX')
#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas

# comment out the following to use wx rather than wxagg
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as
FigureCanvas

from matplotlib.figure import Figure

from wxPython.wx import *

class CanvasFrame(wxFrame):
    
    def __init__(self):
        wxFrame.__init__(self,None,-1,
                         'CanvasFrame',size=(550,350))

        self.SetBackgroundColour(wxNamedColor("WHITE"))

        self.figure = Figure(figsize=(5,4), dpi=100)
        self.axes = self.figure.add_subplot(111)
        t = arange(0.0,3.0,0.01)
        s = sin(2*pi*t)
        
        self.axes.plot(t,s)

        self.canvas = FigureCanvas(self, -1, self.figure)

        self.sizer = wxBoxSizer(wxVERTICAL)
        self.sizer.Add(self.canvas, 1, wxTOP | wxLEFT | wxEXPAND)
        # Capture the paint message
        EVT_PAINT(self, self.OnPaint)

    def OnPaint(self, event):
        self.canvas.draw()
        
class App(wxApp):
    
    def OnInit(self):
        'Create the main window and insert the custom frame'
        frame = CanvasFrame()
        frame.Show(true)

        return true

app = App(0)
app.MainLoop()