wxpython和多个面板中的多个plt图

时间:2016-09-06 07:20:27

标签: python matplotlib wxpython pie-chart

我构建了一个wxpython gui,并试图在两个不同的面板中绘制两个不同的饼图。然而,似乎我一次只能做一个(另一个崩溃)。希望有人知道如何处理这个问题。我也想对条形图做同样的事情。我的代码:

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

1 个答案:

答案 0 :(得分:1)

看看这个minimal wxPython/matplotlib example。成功的关键是充分利用matplotlib-objectoriented接口而不是pyplot接口。避免所有调用pyplot的调用。

而不是:

self.figure, _ = plt.subplot()

使用:

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

在这种情况下,没有必要明确调用canvas.draw