【问题标题】:Matplotlib in wxPython with multiple panelswxPython 中的 Matplotlib 与多个面板
【发布时间】:2015-07-08 07:25:18
【问题描述】:

我正在尝试将此示例转换为多面板示例。

import os
import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as figure
import matplotlib.backends.backend_wxagg as wxagg


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Title')
        #self.create_menu()
        self.create_main_panel()
        self.draw_figure()

    def create_menu(self):
        self.menubar = wx.MenuBar()

        menu_file = wx.Menu()
        m_exit = menu_file.Append(-1, "&Quit\tCtrl-Q", "Quit")
        self.Bind(wx.EVT_MENU, self.on_exit, m_exit)
        self.menubar.Append(menu_file, "&File")
        self.SetMenuBar(self.menubar)

    def create_main_panel(self):
        """ Creates the main panel with all the controls on it:
             * mpl canvas
             * mpl navigation toolbar
             * Control panel for interaction
        """
        self.panel = wx.Panel(self)

        # Create the mpl Figure and FigCanvas objects.
        # 5x4 inches, 100 dots-per-inch
        #
        self.dpi = 100
        self.fig = figure.Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig)
        self.axes = self.fig.add_subplot(111)

        # Create the navigation toolbar, tied to the canvas
        #
        self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)

        #
        # Layout with box sizers
        #
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        #self.vbox.AddSpacer(25)
        self.vbox.Add(self.toolbar, 0, wx.EXPAND)

        self.panel.SetSizer(self.vbox)
        self.vbox.Fit(self)

    def draw_figure(self):
        """ Redraws the figure
        """
        # clear the axes and redraw the plot anew
        #
        self.axes.clear()
        x, y = np.random.random((10, 2)).T
        self.axes.scatter(x, y)

        self.canvas.draw()

    def on_exit(self, event):
        self.Destroy()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    app.frame = MyFrame()
    app.frame.Show()
    app.MainLoop()

这是我目前所拥有的:

import os
import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as figure
import matplotlib.backends.backend_wxagg as wxagg

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(1000,700),style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

            self.create_main_panel()
            self.draw_figure()

        def create_main_panel(self):

            self.hbox = wx.BoxSizer(wx.HORIZONTAL)

            self.left = wx.BoxSizer(wx.VERTICAL)
            self.right = wx.BoxSizer(wx.VERTICAL)

            self.pnl1 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
            self.pnl2 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
            self.pnl3 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
            self.pnl4 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)


            self.fig = figure.Figure((5.0, 4.0), dpi=100)
            self.canvas = wxagg.FigureCanvasWxAgg(self.pnl4, -1, self.fig)
            self.axes = self.fig.add_subplot(111)

            self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)

            self.right.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            #self.right.AddSpacer(25)
            self.right.Add(self.toolbar, 0, wx.EXPAND)

            self.left.Add(self.pnl1, 1, wx.EXPAND | wx.ALL, 3)
            self.left.Add(self.pnl2, 1, wx.EXPAND | wx.ALL, 3)
            self.right.Add(self.pnl3, 1, wx.EXPAND | wx.ALL, 3)
            #right.Add(pnl4, 1, wx.EXPAND | wx.ALL, 3)

            self.hbox.Add(self.left, 1, wx.EXPAND)
            self.hbox.Add(self.right, 1, wx.EXPAND)

            #self.SetSize((400, 120))
            self.SetSizer(self.hbox)
            self.Centre()

        def draw_figure(self):

            self.axes.clear()
            x, y = np.random.random((10, 2)).T
            self.axes.scatter(x, y)
            self.canvas.draw()

        def on_exit(self, event):
            self.Destroy()

    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1, 'borders.py')
            frame.Show(True)
            return True

    app = MyApp(0)
    app.MainLoop()

我尝试从其他示例中提取代码。我已经尝试将图形更深入地嵌入到尺寸器、对话框、框架中。似乎无论我做什么,如果 wxPython 唯一尝试做的事情,我只能得到一个 matplotlib fig 来绘制。 我根本无法让 wxpython 和 matplotlib 做多个框或面板。

我没有足够的声望点来放入图片,但第一个代码导致:http://i.stack.imgur.com/erhUL.png 第二个给出:http://i.stack.imgur.com/6ozk2.png

【问题讨论】:

    标签: matplotlib wxpython


    【解决方案1】:

    首先:在处理示例中的大小/布局问题时,请包含 wx.lib.inspection 模块。

    在主循环前一行插入:

    import wx.lib.inspection as wxli
    wxli.InspectionTool().Show()
    

    运行您的示例。当您单击左侧的树结构(“Widget 树”)时,面板 1 - 3 看起来很合理。但是你会注意到,画布和工具栏没有包含在任何 box sizer 中,这很麻烦。

        # add a sizer for panel 4
        pnl4_sz = wx.BoxSizer(wx.VERTICAL)
        # …
        # add your mpl stuff
        pnl4_sz.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        assert wx.EXPAND == wx.GROW # small joke
        # …
        pnl4_sz.Add(self.toolbar, 0, wx.EXPAND)
        self.pnl4.SetSizer(pnl4_sz)
    
        # …
        # re-instate addition of pnl4
        self.right.Add(self.pnl4, 1, wx.EXPAND | wx.ALL, 3)
    

    【讨论】:

    • 谢谢,效果很好。我也让它在某种程度上与分离器窗口一起工作,但这看起来更好。
    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    相关资源
    最近更新 更多