我是 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 部分时,我做错了什么?有什么更好的方法吗?
提前致谢。欢迎任何想法。