可以在wxPython面板中嵌入matplotlib条形图,但不能在饼图中嵌入

时间:2017-03-03 21:46:34

标签: matplotlib wxpython

我是matplotlib的新手。我发现了许多将条形图和绘图嵌入到wxPython面板中的示例。当我尝试用饼图替换图形或图表时,饼图会显示,并且只有当它关闭时才会显示面板。它没有嵌入。我一直在寻找2天。

我想将thisthis饼图嵌入this示例中。

我无法获取饼图的代码并替换示例中的图形代码。 (这意味着我错过了一些非常大的东西)

这就是我的尝试:

import wx
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import matplotlib.pyplot as plt

class MainFrame(wx.Frame):
    """docstring for MainFrame"""
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title=">>>> by www.UmarYusuf.com", size=(800, 580))

    # Add SplitterWindow panels
    self.split_win = wx.SplitterWindow(self)
    self.top_split = MatplotPanel(self.split_win)
    self.bottom_split = wx.Panel(self.split_win, style=wx.SUNKEN_BORDER)
    self.split_win.SplitHorizontally(self.top_split, self.bottom_split, 480)

    # Add some contrls/widgets (StaticText and Buttons)
    # Add Text control to the bottom_split window
    self.text1 = wx.StaticText(self.bottom_split, -1, u"You can also plot from file", size=(250, 30), pos=(510, 10), style=wx.ALIGN_CENTER)
    self.text1.SetBackgroundColour('Gray')
    font = wx.Font(15, wx.SWISS, wx.NORMAL, wx.NORMAL)
    self.text1.SetFont(font)

    # Add Buttons to the bottom_split window and bind them to plot functions
    self.Button1 = wx.Button(self.bottom_split, -1, "Plot1", size=(80, 40), pos=(10, 10))
    self.Button1.Bind(wx.EVT_BUTTON, self.plot1)

    self.Button2 = wx.Button(self.bottom_split, -1, "Plot2", size=(80, 40), pos=(110, 10))
    self.Button2.Bind(wx.EVT_BUTTON, self.plot2)

    self.Button3 = wx.Button(self.bottom_split, -1, "Plot3", size=(80, 40), pos=(210, 10))
    self.Button3.Bind(wx.EVT_BUTTON, self.plot3)


def plot1(self, event):
    pass

def plot2(self, event):
    pass

def plot3(self, event):
    pass

def plot4(self, event):
    pass

def plot5(self, event):
    pass


class MatplotPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent,-1,size=(50,50))

    self.figure = Figure()

    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    sizes = [15, 30, 45, 10]
    explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

    plt.show()
    self.canvas = FigureCanvas(self, -1, self.figure)


app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()

谢谢。

1 个答案:

答案 0 :(得分:0)

有一个关于embedding matplotlib into wxpython的问题,答案显示的是最小的例子。 matplotlib examples page还提供了几个示例。

您面临的主要问题是您正在创建两个不同的数字。第一个self.figure = Figure()是连接到画布的那个,但是第二个,fig1是您绘制图形的那个。第二个是调用plt.show()时在新窗口中显示的那个。 但是,嵌入GUI时调用plt.show()没有任何意义,因为GUI应该显示图形。

因此问题中的MatplotPanel类应如下所示:

class MatplotPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent,-1,size=(50,50))

        self.figure = Figure()
        labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
        sizes = [15, 30, 45, 10]

        ax1 = self.figure.add_subplot(111)
        ax1.pie(sizes, labels=labels, autopct='%1.1f%%')
        ax1.axis('equal')
        self.canvas = FigureCanvas(self, -1, self.figure)