Possible bug in wxPython or matplotlib.pyplot?

51 views
Skip to first unread message

mbre...@gmail.com

unread,
Jun 26, 2022, 1:12:45 PM6/26/22
to wxPython-users
Dear all

I ran into an issue with wxPython and matplotlib.pyplot. Please take a look at the attached code example. It runs fine as it is on my Linux machine. However, if I uncomment the plt.subplots_adjust(...) line, it hangs at exit.

Am I doing something wrong, or is this a bug? If it is a bug, is it in wxPython or in matplotlib?

wxPython version 4.0.7
matplotlib version 3.5.2
Debian SID

------------------------------------
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

import matplotlib.pyplot as plt

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure

import wx

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes1 = self.figure.add_subplot(2,1,1)
        self.axes2 = self.figure.add_subplot(2,1,2)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()
       
        ### plt.subplots_adjust(hspace=0.4) # <-- This causes trouble!
       
    def draw(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes1.plot(t, s)
        self.axes2.plot(t, -s)


if __name__ == "__main__":
    app = wx.App()
    fr = wx.Frame(None, title='test')
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()
------------------------------------

Matt Newville

unread,
Jun 26, 2022, 2:21:09 PM6/26/22
to wxpytho...@googlegroups.com

I suggest removing
    import matplotlib.pyplot as plt
completely and not using the `pyplot` interface at all.  

You already have an instance of a Figure, so you should be able to replace
    plt.subplots_adjust(hspace=0.4)

with
   self.figure.subplots_adjust(hspace=0.4)



--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/c2cb8664-bd10-4875-b5c3-cf2d5cda5dbfn%40googlegroups.com.


--Matt 

Matthias Brennwald

unread,
Jul 19, 2022, 9:24:10 AM7/19/22
to wxpytho...@googlegroups.com
Hi Matt

Thanks, your suggested fix works perfectly.

Still, there seems to be something wrong, either with what I did in my original example, or with matplotlib, or with wxPython. I'd like to underststand where the problem is. Any thoughts?

Matthias

Matt Newville

unread,
Jul 19, 2022, 12:30:12 PM7/19/22
to wxpytho...@googlegroups.com
On Tue, Jul 19, 2022 at 8:24 AM Matthias Brennwald <mbre...@gmail.com> wrote:
Hi Matt

Thanks, your suggested fix works perfectly.

Still, there seems to be something wrong, either with what I did in my original example, or with matplotlib, or with wxPython. I'd like to underststand where the problem is. Any thoughts?

Well, what seems to be wrong?  

Just in general, if you're asking for help on a forum like this, you'll want to post a minimal working example of real code and then ask about that code, including complete error messages or describing in detail what you expect to happen. 

--Matt

Reply all
Reply to author
Forward
0 new messages