wxPython中具有多个面板的Matplotlib
我试图把这个例子变成一个多面板的例子wxPython中具有多个面板的Matplotlib,matplotlib,wxpython,Matplotlib,Wxpython,我试图把这个例子变成一个多面板的例子 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.Fra
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()
我试着从其他示例中提取代码。我尝试将图形更深地嵌入到大小器、对话框和框架中。似乎无论我做什么,我只能得到一个matplotlib图来绘制,如果这是wxPython试图做的唯一事情。
我无法让wxpython和matplotlib执行多个框或面板
我没有足够的声誉点数来放入图片,但第一个代码会导致:
第二个给出:第一个:在处理像您的示例中那样的大小调整/布局问题时,包括
wx.lib.inspection
模块
在主循环插入之前的行中:
import wx.lib.inspection as wxli
wxli.InspectionTool().Show()
运行您的示例。当你点击左边的树结构(“小部件树”),面板1-3看起来很合理。但您会注意到,画布和工具栏并没有包含在任何框大小器中,这会带来麻烦
# 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)
谢谢,效果很好。我也让它与拆分窗口一起工作,但这看起来更好。