1

Problem:
First, I create figure A with matplotlib.pyplot.subplots. After plotting the data, the figure will be saved.
Next, I call matplotlib.pyplot.clf, before I create a second figure B (also with matplotlib.pyplot.subplots). However, this time I use matplotlib.pyplot.show to display fig B. Unfortunately matplotlib creates two windows, one for A and one for B, instead only one for B (see Screenshot).

Qustion:
Is there a way to block the window of figure A?

System information
OS: Windows 10 (64bit)
Python version: 3.7.3
Matplotlib version: 3.1.0

Sample code

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi, np.pi, 100)

# Creation of figure A
fig, (ax0, ax1) = plt.subplots(2, 1)
ax0.plot(x, np.sin(x))
ax1.plot(x, np.cos(x))
fig.savefig('test.pdf')
# Removing current figure (should be figure A)
plt.clf()

# Creating second figure B
fig2, (ax_0, ax_1) = plt.subplots(2, 1)
ax_0.plot(x, np.sin(x))
ax_1.plot(x, np.cos(x))

plt.show() # Creates two windows, although I am only interested in fig2

beckstev
  • 83
  • 8
  • try using `plt.close()` – dubbbdan Oct 23 '19 at 13:17
  • Thanks it works. I did not know about `plt.close`. This is an informative post explaining the difference betweeen `plt.clf()` and `plt.close`: https://stackoverflow.com/questions/16661790/difference-between-plt-close-and-plt-clf – beckstev Oct 23 '19 at 13:21

0 Answers0