Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Closed
Description
Bug report
Bug summary
The bug revolves around the interaction between tkinter and matplotlib, while using PdfPages. Specifically, somehow a call topyplot.close()
let's the current 'module' finish but afterwards the tk.Tk() instance is closed without warning/error (see the attached MVE code). I expect that this is a bug in the matplotlib domain after hearing that this occurs more often (https://stackoverflow.com/q/54772180/1093485).
Code for reproduction
importtkinterastkimportmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.backends.backend_pdfimportPdfPagesfrompathlibimportPathclassPdf(object):def__init__(self,master):self.master=masterpdf=PdfPages(Path.cwd()/'demo.pdf')self.pdf=pdfdefplot_initial(self):fig=plt.figure(figsize=(8,6))fig.add_subplot(111)mu,sigma=0,0.1s=np.random.normal(mu,sigma,1000)count,bins,ignored=plt.hist(s,30,density=True)plt.plot(bins,1/(sigma*np.sqrt(2*np.pi))*np.exp(- (bins-mu)**2/ (2*sigma**2) ),linewidth=2,color='r')plt.title('Overview')plt.xlabel('X')plt.ylabel('Y')self.pdf.savefig(fig)# THE CULPRITplt.close(fig)defplot_extra(self):fig=plt.figure(figsize=(8,6))fig.add_subplot(111)mu,sigma=0,0.1s=np.random.normal(mu,sigma,1000)count,bins,ignored=plt.hist(s,30,density=True)plt.plot(bins,1/(sigma*np.sqrt(2*np.pi))*np.exp(- (bins-mu)**2/ (2*sigma**2) ),linewidth=2,color='r')plt.title('Extra')plt.xlabel('X')plt.ylabel('Y')self.pdf.savefig(fig)plt.close(fig)defclose(self):self.pdf.close()classMVE(object):@classmethoddefrun(cls):root=tk.Tk()MVE(root)root.mainloop()def__init__(self,master):self.root=mastertk.Frame(master)menu=tk.Menu(master)master.config(menu=menu)test_menu=tk.Menu(menu,tearoff=0)menu.add_cascade(label='Bug',menu=test_menu)test_menu.add_command(label='PDF',command=self.generate_pdf)defgenerate_pdf(self):pdf=Pdf(self)pdf.plot_initial()foriinrange(0,3):pdf.plot_extra()pdf.close()if__name__=="__main__":MVE.run()
Expected outcome
I expect that the initial tk.Tk() window isn't closed once the PDF report has been generated.
Matplotlib version
- Operating system: Tested on Win 7 and Win 10
- Matplotlib version: 2.2.3 from pip
- Matplotlib backend (
print(matplotlib.get_backend())
): TkAgg - Python version: 3.7.0
- Jupyter version (if applicable):
- Other libraries: Tkinter 8.6 from pip