Difference between plt.draw() and plt.show() in matplotlib

Question:

I was wondering why some people put a plt.draw() into their code before the plt.show(). For my code, the behavior of the plt.draw() didn’t seem to change anything about the output. I did a search on the internet but couldn’t find anything useful.

(assuming we imported pyplot as from matplotlib import pyplot as plt)

Asked By: user2489252

||

Answers:

plt.show() will display the current figure that you are working on.

plt.draw() will re-draw the figure. This allows you to work in interactive mode and, should you have changed your data or formatting, allow the graph itself to change.

The plt.draw docs state:

This is used in interactive mode to update a figure that has been altered using one or more plot object method calls; it is not needed if figure modification is done entirely with pyplot functions, if a sequence of modifications ends with a pyplot function, or if matplotlib is in non-interactive mode and the sequence of modifications ends with show() or savefig().

This seems to suggest that using plt.draw() before plt.show() when not in interactive mode will be redundant the vast majority of the time. The only time you may need it is if you are doing some very strange modifications that don’t involve using pyplot functions.

Refer to the Matplotlib doc, "Interactive figures" for more information.

Answered By: Ffisegydd
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.