Based on the examples I expect that calling plt.ion() will turn on interactive mode so that plt.show() doesn't block. However when I call this the plot never shows up (the python app launches, but there is no window). Commenting out the ion() shows the plot and it blocks on show(). Am I doing something wrong, or is this just broken?
import time
from matplotlib import pyplot as plt
plt.figure()
plt.ion()
plt.plot(range(5))
plt.show()
while 1:
time.sleep(1)
Versions:
2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)]
matplotlib-1.5.3-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
TL; DR - this is the correct and expected behavior
That is not quite how the integration with the GUI works.
There is a stalled PR adding documentation for this #4779
The very short version is that in order for the GUI to be responsive there must be an event loop running to respond to the user input. The reason that plt.show blocks is that the GUI event loop has taken over the process and is (mostly) sitting idle waiting for you to poke the GUI.
By using plt.ion() you are telling the GUI framework to not take over the the process and just to show the window and move on. This is for use in IPython (or the plain python prompt if you must, but we strongly recommend working in IPython) where there is a hook set up such that while the prompt is sitting there waiting for you to type lets the event loop spin so the plot is interactive and you have the prompt. You will note that if you sleep for 30s, the plot will be frozen during that time.
Activity
tacaswell commented on Oct 25, 2016
TL; DR - this is the correct and expected behavior
That is not quite how the integration with the GUI works.
There is a stalled PR adding documentation for this #4779
The very short version is that in order for the GUI to be responsive there must be an event loop running to respond to the user input. The reason that plt.show blocks is that the GUI event loop has taken over the process and is (mostly) sitting idle waiting for you to poke the GUI.
By using plt.ion() you are telling the GUI framework to not take over the the process and just to show the window and move on. This is for use in IPython (or the plain python prompt if you must, but we strongly recommend working in IPython) where there is a hook set up such that while the prompt is sitting there waiting for you to type lets the event loop spin so the plot is interactive and you have the prompt. You will note that if you sleep for 30s, the plot will be frozen during that time.
aholtzma commented on Oct 25, 2016
Can I spawn a thread and run the event loop independently?
tacaswell commented on Oct 27, 2016
GUI event loops tend to require they be on the main thread.
This discussion will be better on matplotlib-users@python.org (you will have to join the list to post un-moderated) than in this issue.