0

我是 matplotlib 的新手,遇到了一些困难。从用户的角度来看,一切正常。我有一个带按钮的窗口。按下按钮会弹出一个显示绘图的新窗口。玩完之后,我点击 X 关闭这个窗口。并再次单击 X 以使用该按钮关闭窗口。但我没有退回到免费提示。主窗口被锁定并仍在后台运行。

这是代码的重要部分:

import matplotlib
import matplotlib.pyplot as Plt
from matplotlib.figure import Figure

class MainPanel(wx.Panel):

    def __init__(self, parent): #=None

        wx.Panel.__init__(self, parent)

                       .... wxpython stuff goes here....

              self.btn1.Bind(wx.EVT_BUTTON, self.cplot)

                       ....

    def cplot(self, event):
        self.new = NewWindow(self)
        self.new.cidadeplot(self)
        self.new.Show()
        return


class NewWindow(wx.Frame):

    def __init__(self,event):
        wx.Frame.__init__(self, None, -1, 'Plot', size=(556, 618))
        wx.Frame.CenterOnScreen(self)
        self.Bind(wx.EVT_CLOSE, self.OnClose)


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


    def cidplot(self,event):


        self.fig = Figure()

        self.axes = self.fig.add_subplot(111)
        self.canvas = FigCanvas(self, -1, self.fig)

        self.axes.set_ylabel("Parts")
        self.axes.set_ylim(0,100)
        self.axes.grid(True)

        ....more axes settings...

       bars = self.axes.bar(left=self.ii, height=self.tt, width=0.2, align='center', alpha=0.8)

        ....

        self.canvas.draw()

        Plt.savefig("Parts.pdf", dpi=300)



class MainFrame(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title = "System Test")

        self.SetSize((556, 618))

        panel = MainPanel(self)

        self.Show()


if __name__ == "__main__":

    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

我认为问题出在 self.fig = Figure() (如果我使用 Plt.figure() 也会发生同样的情况)

当我创建一个新窗口或在我的 matplotlib 部分时,我做错了什么?有什么更好的方法吗?

提前致谢。欢迎任何想法。

4

3 回答 3

1

不要导入pyplot,它有自己的包装来处理管理图形 gui 框架(除了状态机界面)是pyplot. 要做到这一点,它会启动它自己的主循环,这可能是让你搞砸的原因。

请务必查看此处的示例

于 2013-04-17T22:41:41.583 回答
0

使用此功能 OnClose:

def onClose(self, event):
    """"""
    self.Close(True)
于 2013-04-19T17:28:07.887 回答
0

特卡斯韦尔是对的。

我删除了对 pyplot 的所有引用。savefig 正在锁定应用程序。全部更改为 self.axes.XXX 和 salf.fig.savefig(xxx) 并工作。

谢谢。

于 2013-04-22T14:57:45.460 回答