Search code examples
pythonmatplotlibwxpythonpie-chart

wxpython and multiple plt plots in multiple panels


I built a wxpython gui and am trying to plot two different pie charts in two different panels. However it seems that i can only do one at a time (the other one crashes). Hoping someone might know how to handle this. I also want to do the same with bar charts. My code:

self.V_Panel_Pie1 = FigurePanel(self.V_Panel7)
self.V_Panel_Pie2 = FigurePanel(self.V_Panel8)

sizer_vpanel = wx.BoxSizer(wx.VERTICAL)
sizer_vpanel.Add(self.V_Panel_Pie1,1)
self.V_Panel7.SetSizer(sizer_vpanel)

sizer_vpanel = wx.BoxSizer(wx.VERTICAL)
sizer_vpanel.Add(self.V_Panel_Pie2,1)
self.V_Panel8.SetSizer(sizer_vpanel)

self.V_Panel_Pie1.draw(a_vals, b_vals)
self.V_Panel_Pie2.draw(a_vals2, b_vals2)

class FigurePanel(wx.Panel):
    def __init__(self, parent):

        wx.Panel.__init__(self, parent)
        self.parent = parent
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.figure, ax = plt.subplots(figsize=(2,2))
        self.canvas = FigureCanvas(self, -1, self.figure)
        s1 = wx.BoxSizer(wx.VERTICAL)
        s1.Add(self.canvas, 0, wx.GROW)
        self.sizer.Add(s1, 5, wx.GROW)

        self.SetSizer(self.sizer)
        self.Layout()
        self.Fit()

    def draw(self, a, b):

        self.figure.clear()
        labels = 'a', 'b'
        sizes = [a,b]
        colors = ['yellowgreen', 'lightskyblue']
        explode = (0, 0.1)

        plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
        self.canvas.draw()here

Solution

  • Look at this minimal wxPython/matplotlib example. The Key to success is to take fully use of the matplotlib-objectoriented interface instead of the pyplot interface. Avoid all call invoking pyplot.

    Instead of:

    self.figure, _ = plt.subplot()
    

    use:

    # from matplotlib.figure import Figure
    self.figure = Figure()
    self.axes = self.figure.add_subplot(211)
    self.axes2 = self.figure.add_subplot(212)
    
    # to draw the pies
    self.axes.pie(...)
    self.axes2.pie(...)
    

    It is not necessary to call canvas.draw explicitly in this case.