Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Matplotlib.pyplot.close() interacts with tk.Tk() and closes the tk.Tk() object. #13470

Closed
Labels
GUI: tkGood first issueOpen a pull request against these issues if there are no active ones!
@Tarskin

Description

@Tarskin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    GUI: tkGood first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp