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

can embed matplotlib bar graph in wxPython panel but not pie chart

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

我想嵌入 this or this pie chart into 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()

谢谢。

有一个关于 embedding matplotlib into wxpython, where the answer is showing a minimal example. Several examples are also available on the matplotlib examples page 的问题。

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

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

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)