【问题标题】:Matplotlib bar chart in a wx Frame instead of a new windowwx 框架中的 Matplotlib 条形图而不是新窗口
【发布时间】:2013-02-12 01:46:12
【问题描述】:

我有一个简单的 wxFrame 和两个面板。我希望其中一个面板显示 matplotlib 条形图。我学会了使用图表,但我使用的 show() 函数在新窗口中提供了图表。

 import wx
import wx.grid as gridlib
import os
import Image
import pylab as p

class PanelOne(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        #txt = wx.TextCtrl(self)
        button =wx.Button(self, label="Save", pos=(200, 325))
        button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels)

class PanelTwo(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)


        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        fig=p.figure()
        ax=fig.add_subplot(1,1,1)
        x=[1,2,3]
        y=[4,6,3]
        ax.bar(x,y)
        p.show()

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "Panel Switch",
                          size=(800,600))

        self.panel_one = PanelOne(self)
        self.panel_two = PanelTwo(self)
        self.panel_two.Hide()

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.panel_one, 1, wx.EXPAND)
        self.sizer.Add(self.panel_two, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        menubar = wx.MenuBar()
        fileMenu = wx.Menu()
        switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
                                                  "Switch Panels",
                                                  "Some text")
        self.Bind(wx.EVT_MENU, self.onSwitchPanels,
                  switch_panels_menu_item)
        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)
-------------------------------------------------------------------
    def onSwitchPanels(self, event):

        if self.panel_one.IsShown():
           self.SetTitle("Panel Two Showing")
           self.panel_one.Hide()
           self.panel_two.Show()
        else:
           self.SetTitle("Panel One Showing")
           self.panel_one.Show()
           self.panel_two.Hide()
        self.Layout()

    if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

我在一个新的 matplotlib 窗口中得到图表,当我关闭它时,我的框架出现了。我需要图表位于框架的第二个面板中。

我尝试阅读 This ,但对我没有太大帮助。

【问题讨论】:

    标签: python-2.7 matplotlib wxpython


    【解决方案1】:

    您正在使用的 pyplot 函数 (p.figure) 的主要目的是处理所有 GUI 细节(与您想要的不同用途)。您想要将matplotlib 嵌入到您现有的gui 中。有关如何执行此操作,请参阅这些examples

    此代码是从embedding_in_wx2.py 提取的,以防止链接腐烂:

    #!/usr/bin/env python
    """
    An example of how to use wx or wxagg in an application with the new
    toolbar - comment out the setA_toolbar line for no toolbar
    """
    # Used to guarantee to use at least Wx2.8
    import wxversion
    wxversion.ensureMinimal('2.8')
    from numpy import arange, sin, pi
    
    import matplotlib
    
    # uncomment the following to use wx rather than wxagg
    #matplotlib.use('WX')
    #from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
    
    # comment out the following to use wx rather than wxagg
    matplotlib.use('WXAgg')
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    
    from matplotlib.backends.backend_wx import NavigationToolbar2Wx
    
    from matplotlib.figure import Figure
    import wx
    
    class CanvasFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self,None,-1,
                             'CanvasFrame',size=(550,350))
    
            self.SetBackgroundColour(wx.NamedColour("WHITE"))
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            t = arange(0.0,3.0,0.01)
            s = sin(2*pi*t)
            self.axes.plot(t,s)
            self.canvas = FigureCanvas(self, -1, self.figure)
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.SetSizer(self.sizer)
            self.Fit()
    
        def OnPaint(self, event):
            self.canvas.draw()
    
    class App(wx.App):
        def OnInit(self):
            'Create the main window and insert the custom frame'
            frame = CanvasFrame()
            frame.Show(True)
            return True
    
    app = App(0)
    app.MainLoop()
    

    【讨论】:

    • 这适用于框架。但我正在寻找将图表嵌入其中一个面板。还有条形图。我在面板内尝试了您的代码,但失败了。由于无法调出条形图,我使用了 pylot。
    • 我对“失败”无能为力。您应该使用 exactly 您尝试过的内容或打开一个新问题来编辑您的问题。要获得条形图而不是线条,只需更改 self.axes.plot(...) -> self.axes.bar(...)
    • 实际上我稍微调整了一下,并将情节更改为你说的 bar 并且效果很好。谢谢。
    • 很高兴听到!抱歉,我太贪心了,今天的咖啡还没喝够。
    • 没问题。帮助很大。我只是在学习 python 并试图在一个窗口中获得很多图。 :) 我也需要咖啡。干杯。
    猜你喜欢
    • 2022-11-18
    • 2023-03-06
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多