wxpython and multiple plt plots in multiple panels
<p>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:</p>

<pre><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
</code></pre>

<p>Look at this <a href="http://stackoverflow.com/questions/10737459/embedding-a-matplotlib-figure-inside-a-wxpython-panel">minimal wxPython/matplotlib example</a>. The Key to success is to take fully use of the matplotlib-objectoriented interface instead of the pyplot interface. Avoid all call invoking pyplot.</p>

<p>Instead of:</p>

<pre><code>self.figure, _ = plt.subplot()
</code></pre>

<p>use:</p>

<pre><code># 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(...)
</code></pre>

<p>It is not necessary to call <code>canvas.draw</code> explicitly in this case.</p>

python matplotlib wxpython pie-chart