I've read When to use cla(), clf() or close() for clearing a plot in matplotlib? But still I couldn't find a nice solution to my problem.
I've a lot of data that needs to be plotted and saved to individual png or jpg file. Something like below but it really takes some memory and time to finish.
Is there anything that can be optimized? I'm thinking the background and the interface style, because I don't need the windows to display my figure, just save it would be enough.
Update: I tried to move plt.subplots() part outside the main loop. But then I can only get the full figure(axes,title and the plot line) for the first one. After the first pic, all pics have only axes and title but no data line.
Disclaimer: I'm saying that I don't need the picture to show. The script doesn't open up any window.
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def plot(tup):
k, name, x, y = tup
fig, ax = plt.subplots()
ax.plot(x, y)
for _x, _y in zip(x, y):
ax.annotate(str(_y), (_x, _y + 0.2))
fig.savefig('pics/{}.png'.format(k))
fig.clf()
for e in alldata:
plot(e)