Assertion triggered in wxMemory DC

Some more info. I'm running a 1.8Ghz PIV laptop with windows XP
professional. Not sure if speed of machine has anything todo with it. The
repaints cause flicker on the window, and then after a while the messages
appear.

···

-----Original Message-----
From: Schalkwyk, Johan
Sent: Tuesday, June 29, 2004 11:25 AM
To: 'John Hunter'; Schalkwyk, Johan
Cc: matplotlib-users@lists.sourceforge.net
Subject: RE: [Matplotlib-users] Assertion triggered in wxMemory DC

With the attached example code you sent me I get the following messages
after some time. It seems to be caught in some form of endless loop, just
scrolling the messages over and over.

Also thank you for the example. I can simply my app code much more now.
Where can I find the src distribution. I probably did not look properly on
the web site.

Johan

line 570, in new_gc
    self.gc = GraphicsContextWx(self.bitmap, self)
  File
"c:\Tools\Python23\lib\site-packages\matplotlib\backends\backend_wx.py",
line 682, in __init__
    self.SelectObject(bitmap)
  File "C:\Tools\Python23\Lib\site-packages\wx\gdi.py", line 3017, in
SelectObje
ct
    return _gdi.MemoryDC_SelectObject(*args, **kwargs)
wx.core.PyAssertionError: C++ assertion "wxAssertFailure" failed in
..\..\src\ms
w\dcmemory.cpp(133): Couldn't select a bitmap into wxMemoryDC
Traceback (most recent call last):
  File "mattest2.py", line 42, in OnPaint
    self.canvas.draw()
  File
"c:\Tools\Python23\lib\site-packages\matplotlib\backends\backend_wx.py",
line 921, in draw
    self.figure.draw(self.renderer)
  File "c:\Tools\Python23\lib\site-packages\matplotlib\figure.py", line 132,
in
draw
    if self.frameon: self._figurePatch.draw(renderer)
  File "C:\Tools\Python23\Lib\site-packages\matplotlib\patches.py", line 54,
in
draw
    gc = renderer.new_gc()
  File
"c:\Tools\Python23\lib\site-packages\matplotlib\backends\backend_wx.py",
line 570, in new_gc

-----Original Message-----
From: John Hunter [mailto:jdhunter@…4…]
Sent: Tuesday, June 29, 2004 10:47 AM
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,> Thank you for the help I removed the show() command
    Schalkwyk,> from the loop. Now just calling grid(True) multiple
    Schalkwyk,> times. On my machine I narrowed the loop to 57 and it
    Schalkwyk,> started happening. At 50 it did not.

Perhaps I didn't make myself clear. The way you are using matplotlib
does not make sense. matplotlib has two modes: a "matlab interface"
and an embedded interface. You should not embed the matlab interface
directly into a GUI. The matlab interface does a lot of stuff under
the hood, like creating and managing figures and figure windows,
managing redraws, etc. When you create your own figure window outside
this framework, and then try to use the framework, the result is
undefined. If you are using matplotlib in a GUI, *do not import
matplotlib.matlab*. You need to follow the example of
embedding_in_wx2.py.

Below is your example translated to the GUI interface - on my system,
calling grid until the sun sets presents no problems, and it really
couldn't because all it does is set a boolean. In the matlab
interface, if you have interactive: True in your rc file, calling grid
does a lot more, including repainting the figure. Try running this
example, shaking it, resizing it, etc... and see if you can crash it;
I was unable to cause any problems.

If you have trouble, please let me know, but also consider trying
replacing wx for wxagg, which uses antigrain for drawing and is
probably more stable than wx, which does occasionally show dc related
errors.

By the way, there was an error in your legend code

You had

  legend('line', blahblah)

and you need

  legend( ('line',), blahblah)

That is, legend expects a list or tuple of strings, not a string.

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

Thanks. Do you have the src distribution? There is an example in the
examples directory examples/coords_demo.py (works with Tk, GTK and WX)
that shows you how to get the mouse click coordinates. The examples
dir is your first line of defense when you want to figure out
something new. Unfortunately, I forgot to upload the zip file with
the 0.54.2 release, so look here

http://matplotlib.sf.net/examples

Here's the example:

#!/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
matplotlib.use('WX')
from matplotlib.backends.backend_wx import FigureCanvasWx 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.ax = self.figure.add_subplot(111)
        t = arange(0.0, 2.0, 0.01)
        s = sin(2*pi*t)
        l = self.ax.plot(t, s, linewidth=1.0)
        self.ax.set_xlabel('time (s)')
        self.ax.set_ylabel('voltage (mV)')
        self.ax.set_title('About as simple as it gets, folks')
        self.ax.legend(('line',), loc='upper right')

        for i in range(100):
            self.ax.grid(True)
        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()