如何在wx.Notebook面板中为matplotlib FigureCanvas更正布局

时间:2012-06-21 18:44:53

标签: wxpython matplotlib wxnotebook

我几天来一直在努力解决这个问题。如果我不使用wx.Notebook,我可以在sizer中正确地调整ImageCanvas的大小,但是一旦我将FigureCanvas放在wx.Notebook面板中,该图似乎忽略了面板大小调整器。它也有一些刷新问题,如果你将它从屏幕上拉下来并重新打开,情节就会消失。也许命令的顺序有问题,但我似乎无法找到它。这篇文章:How to update a plot with python and Matplotlib帮助我做到了这一点,但我似乎无法弄清楚为什么一旦我在wx.Notebook中使用它,情节会有不同的行为。

以下是首次显示时的样子:http://static.inky.ws/image/2205/image.jpg

这是在我中途退出屏幕之后:http://static.inky.ws/image/2206/image.jpg

#!/usr/bin/env python
import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self,
                          parent,
                          id=wx.ID_ANY,
                          pos=wx.DefaultPosition,
                          size=wx.Size( -1,-1 ),
                          style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)

        fgSizer8 = wx.FlexGridSizer(0, 1, 0, 0)
        self.m_notebook2 = wx.Notebook(self,
                                       wx.ID_ANY,
                                       wx.DefaultPosition,
                                       wx.DefaultSize,
                                       0)

        self.m_panel8 = wx.Panel(self.m_notebook2,
                                 wx.ID_ANY,
                                 wx.DefaultPosition,
                                 wx.DefaultSize,
                                 wx.TAB_TRAVERSAL)

        bSizer20 = wx.BoxSizer(wx.VERTICAL)

        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.pts = self.axes.plot([1, 2, 3], [4, 5, 7], 'ro-', picker=5)
        self.canvas = FigureCanvas(self, -1, self.fig)

        bSizer20.Add(self.canvas, 1, wx.ALL|wx.EXPAND, 5)

        self.m_panel8.SetSizer(bSizer20)
        self.m_panel8.Layout()
        bSizer20.Fit(self.m_panel8)
        self.m_notebook2.AddPage(self.m_panel8, u"a page", False)

        fgSizer8.Add(self.m_notebook2, 1, wx.EXPAND |wx.ALL, 5)

        self.SetSizer(fgSizer8)
        self.Layout()
        fgSizer8.Fit(self)

        self.Centre(wx.BOTH)

if __name__ == "__main__":
    app = wx.App(0)
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:3)

变化

self.canvas = FigureCanvas(self, -1, self.fig)

self.canvas = FigureCanvas(self.m_panel8, -1, self.fig)

enter image description here