mahmood
mahmood

Reputation: 24815

Bypassing plt.show() automatically

I use the following code to save multiple plots to individual files.

bench = ['AA', 'BB']
for b in bench:
    ...
    fname = b + '.png'
    plt.savefig(fname)
    plt.show()

Problem is that when I run the code, it brings up the plot for AA, then I have to close it and then it brings up BB. After closing the window, the program returns to prompt.

If I remove, plt.show(), the program terminates without "manual closing". However, in AA.png, I see only one line for AA, but in BB.png I see two lines, AA and BB.

Is there a way to bypass those "manually closing the plot windows"?

Upvotes: 1

Views: 43

Answers (1)

Queuebee
Queuebee

Reputation: 670

Use plt.draw() instead of plt.show()

bench = ['AA', 'BB']
for b in bench:
    # ... and maybe clear the plt here, plt.clf() to not mix AA with BB
    fname = b + '.png'
    plt.savefig(fname)
    plt.draw()

Upvotes: 1

Related Questions