0

In the following example code...

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

with PdfPages('multipage_pdf.pdf') as pdf:
    for i in range(10):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.scatter([0, 1, 2], [0, 1, 2])
        pdf.savefig()
        plt.close()

... every single plot pops up a windows with the actual canvas. Is there an elegant solution to skip the actual drawing of the canvas on the screen and draw the plot directly into a multipage pdf?

PS: Problem only caused when running code within spyder, so related to spyder and not to anything else. Running code directly using python does not cause this popping up of windows.

Sosel
  • 1,678
  • 1
  • 16
  • 31
  • 1
    very useful tool! I cannot reproduce the issue with python 3.6.7 and matplotlib 2.1.2. The pdf is created with nothing drawn on screen... – user2660966 Aug 20 '19 at 07:07
  • With python 3.7.3 (windows) the figure pops up quickly and then I close it at the end. This popping up is what I would like to skip. – Sosel Aug 20 '19 at 07:20

2 Answers2

0

I think what you are looking for is to clear the figure. first_page = plt.figure(figsize=(11.69, 8.27)) first_page.clf() Check out the documentation: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.clf.html

Some examples: https://www.programcreek.com/python/example/102298/matplotlib.pyplot.clf

  • It is not about clearing the figure, but actually ommiting to create a figure (window) at all, plotting directly into the pdf. – Sosel Aug 20 '19 at 07:23
  • Oh that makes more sense to the problem, yes. I used Python 3.7.4 and multipage pdf recently on Windows and I never got a pop-up. I used the PyCharm Editor in which unless I write 'figure.show()' in my code, won't show any plot. It plots it directly into the pdf. –  Aug 20 '19 at 07:36
0

The problem did not arise from matplotlib or anything similar, but just running the code from within Spyder caused this problem. Running the code using python directly does not cause the issues I had before.

Sosel
  • 1,678
  • 1
  • 16
  • 31