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

可以在wxPython面板中嵌入matplotlib条形图,但不能嵌入饼图,matplotlib,wxpython,Matplotlib,Wxpython,我是matplotlib的新手。我发现了许多将条形图和绘图嵌入wxPython面板的示例。当我尝试用饼图替换图形或绘图时,饼图会显示,只有当它关闭时,面板才会显示。它没有嵌入。我已经找了两天了 我想在示例中嵌入一个或多个饼图 我不能将饼图的代码替换为示例中图形的代码。(这意味着我错过了一件大事) 这就是我尝试过的: import wx import matplotlib from matplotlib.figure import Figure from matplotlib.backends.b

我是matplotlib的新手。我发现了许多将条形图和绘图嵌入wxPython面板的示例。当我尝试用饼图替换图形或绘图时,饼图会显示,只有当它关闭时,面板才会显示。它没有嵌入。我已经找了两天了

我想在示例中嵌入一个或多个饼图

我不能将饼图的代码替换为示例中图形的代码。(这意味着我错过了一件大事)

这就是我尝试过的:

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()
谢谢。

有一个关于的问题,答案显示了一个最小的例子。网站上还提供了几个示例

您在这里面临的主要问题是,您正在创建两个不同的图形。第一个,
self.figure=figure()。第二个是调用
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)

这很有帮助。你能推荐一个简单的教程页面吗?我发现matplotlib.org上的教程让我不知所措。也许我用错模块了。我想一个图形表示,即饼图,随着时间的推移用户输入。用户说‘我希望灯光先亮两小时,然后再亮8小时。饼图将显示20%的红色和80%的蓝色。我走对了吗?我个人不知道有什么教程可以推荐,但肯定有一些。此外,matplotlib是您能找到的最好的文档库之一,因此,出于您的目的,使用它似乎是合适的。对于用户交互,matplotlib可以嵌入到wxpython、tkinter或PyQt中。我想说,这些都是不错的选择。所以我认为你在正确的轨道上。当遇到问题时,您需要通过创建最少的示例来缩小问题范围(不仅仅是为了在此处发布,也为了您自己)。你会发现大多数问题都已经有了答案,如果没有,就在这里问吧。谢谢你的好意。