Graph window resized incorrectly with macOS+matplotlib+wxPython

Asked 3 months ago, Updated 3 months ago, 8 views

Using Python 3.8.1, Matplotlib 3.2.1, and wxPython 4.0.7 post2 installed on macOS Catalina (10.15.4), the code below draws four graphs, but if you resize the window, the initial image remains and the enlarged image is not displayed.

Windows will redraw without any problems. How can I modify the code to make it look good on macOS?

import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAggas FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAggas NavigationToolbar
import matplotlib.gridspec as gridspec
import numpy as np

class MainFrame (wx.Frame):
    def__init__(self):
        width, height = wx.GetDisplaySize()
        wx.Frame.__init__(self, None, -1,
                          size = (int(width*0.72), int(width*0.45),
                          pos=(20,20))
        self.ini_figures()

    defini_figures (self):
        self.fig = Figure ((8,6))
        self.canvas = FigureCanvas (self, -1, self.fig)
        self.gs=gridspec.GridSpec(2,2)
        self.sizer=wx.BoxSizer(wx.VERTICAL)
        self.mpl_toolbar = NavigationToolbar (self.canvas)
        self.mpl_toolbar.get_canvas(self,self.fig)
        self.mpl_toolbar.update()
        self.mpl_toolbar.Realize()
        self.sizer.Add (self.canvas, 1, wx.EXPAND, wx.EXPAND)
        self.sizer.Add (self.mpl_toolbar, 0, wx.EXPAND, wx.EXPAND)
        self.SetSizer (self.sizer)
        self.Fit()
        self.drawGraphs (self.fig, self.canvas, self.gs)

    def drawGraphs(self,fig,canvas,gs):
        x = np.linspace (1100)
        y1 = np.exp(x)
        y2 = np.log(x)
        y3 = np.sin (2*np.pi*x)
        y4 = np.cos (2*np.pi*x)

        config.clear()
        self.axis1 = config.add_subplot(gs[0,0])
        self.axis1.plot(x,y1,'+')
        self.axis2 = config.add_subplot(gs[0,1])
        self.axis2.plot(x,y2,'+')
        self.axis3 = config.add_subplot(gs[10])
        self.axis3.plot(x,y3,'+')
        self.axis4=fig.add_subplot(gs[1,1])
        self.axis4.plot(x,y4,'+')
        canvas.draw()

defmain():
    app=wx.App()
    window=MainFrame()
    window.Show()
    app.MainLoop()

if__name__=="__main__":
    main()

python3 macos matplotlib

2022-09-30 13:45

1 Answers

The symptoms may be different, but Windows also experienced a situation where the drawing was disrupted once it was reduced and then enlarged.
64-bit Python 3.8.2, Matplotlib 3.2.1, and wxPython 4.0.7 post2 environment on Windows 10.

So when I looked at the sample below of matplotlib, the setting order of the main graph area and the setting order of the toolbar area were reversed, so I changed it accordingly and it started to work well.
user_interfaces example code:embedding_in_wx2.py

The source of the application is as follows.I have included comments in the changes.
See what happens with macOS.

import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAggas FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAggas NavigationToolbar
import matplotlib.gridspec as gridspec
import numpy as np

class MainFrame (wx.Frame):
    def__init__(self):
        width, height = wx.GetDisplaySize()
        wx.Frame.__init__(self, None, -1,
                          size = (int(width*0.72), int(width*0.45),
                          pos=(20,20))
        self.ini_figures()

    defini_figures (self):
        self.fig = Figure ((8,6))
        self.canvas = FigureCanvas (self, -1, self.fig)
        self.gs=gridspec.GridSpec(2,2)
        self.drawGraphs(self.fig,self.canvas,self.gs)# Draw Graphs First
        self.sizer=wx.BoxSizer(wx.VERTICAL)
        # Toolbars will be added later
        self.sizer.Add (self.canvas, 1, wx.EXPAND, wx.EXPAND)
        self.SetSizer (self.sizer)
        self.Fit()

        Call the sample method in self.add_toolbar()#matplotlib

    def drawGraphs(self,fig,canvas,gs):
        x = np.linspace (1100)
        y1 = np.exp(x)
        y2 = np.log(x)
        y3 = np.sin (2*np.pi*x)
        y4 = np.cos (2*np.pi*x)

        config.clear()
        self.axis1 = config.add_subplot(gs[0,0])
        self.axis1.plot(x,y1,'+')
        self.axis2 = config.add_subplot(gs[0,1])
        self.axis2.plot(x,y2,'+')
        self.axis3 = config.add_subplot(gs[10])
        self.axis3.plot(x,y3,'+')
        self.axis4=fig.add_subplot(gs[1,1])
        self.axis4.plot(x,y4,'+')
        canvas.draw()

    # Copying Matplotlib Samples
    default_toolbar(self):
        self.toolbar=NavigationToolbar(self.canvas)
        self.toolbar.Realize()
        self.sizer.Add (self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.toolbar.update()

defmain():
    app=wx.App()
    window=MainFrame()
    window.Show()
    app.MainLoop()

if__name__=="__main__":
    main()


2022-09-30 13:45

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.