简体   繁体   中英

Show only the last plot in Python using MatPlotLib

I am using Python 3.5 with MatPlotLib package. My problem is as follows: I generate, say 50 plots, which I save each to a PNG file. Then I generate 2 summarizing plots, which I want to both save and show on display. However, when I use the plt.show() command, it also shows all the previous 50 plots, which I don't want to display, just save them. How to suppress the show on these previous 50 plots and show only the last one?

Here is an example code:

import matplotlib.pyplot as plt
import numpy as np

for i in range(50):
   fig = plt.figure()
   plt.plot(np.arange(10),np.arange(10)) # just plot something
   plt.savefig(f"plot_{i}.png")
   # I want to save these plots but not show them
# summarizing plot
fig = plt.figure()
plt.plot(np.arange(100),np.arange(100))
plt.show() # now it shows this fig and the previous 50 figs, but I want only to show this one!

Close all after the loop:

plt.close("all") #this is the line to be added
fig = plt.figure()
plt.plot(np.arange(100),np.arange(100))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM