I am writing a python script with interspersed plots and computations.
I am confused about the behavior of matplotlib and in particular the necessity of plt.pause
. Consider the following snippets:
import matplotlib.pyplot as plt
import time
fig,ax=plt.subplots()
ax.plot([1,2])
fig.show()
time.sleep(5) #This is a substitute for real computations
-> Nothing happens for five seconds
import matplotlib.pyplot as plt
import time
fig,ax=plt.subplots()
ax.plot([1,2])
plt.pause(0.1)
fig.show()
time.sleep(5) #This is a substitute for real computations
-> Window displays the desired plot for five seconds
It seems that plt.pause
is required to see anything. Why then does the documentation say "This function is experimental; its behavior may be changed or extended in a future release." and why did I not see plt.pause
in any tutorials?
Also, why would such an essential function be designed so strangely that the user has to input a small enough time, yet not zero? I get that some people actually want to pause exeuction, but I don't, I just want to see the plots. Is this so unusual?
By the way, I noticed that I can also do plt.show()
, which, for reasons unknown to me, behaves different than plt.gcf().show()
[=fig.show
] and blocks the execution until the user closes the window. While this does show the plot when I want it, I do not want the execution to be stopped and I want the user to keep seeing the plot during the subsequent computations. Using plt.show(block=False)
DOES seem to behave like plt.gcf().show()
[=fig.show()
], so it is also useless.
Furthermore, I read somewhere that plt.ion
should help, but it doesn't. Adding plt.ion()
before fig,ax=plt.subplots()
in the snippets above doesn't change anything.
Finally, I heard that different backends might behave differently. I am using python 3.6 (anaconda) on Ubuntu 18 with matplotlib 2.2.2. If I add import matplotlib; matplotlib.use('Qt5Agg')
at the beginning of the snippets, not much changes, but instead of showing nothing for five seconds the first snippets shows a garbage window for five seconds (the window shows whatever was shown on the screen at the location where it popped up).