Program Not Continuing After Closing Matplotlib Plot
grossmanhaideatel.blogspot.com
Source: https://www.techtalk7.com/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue/
grossmanhaideatel.blogspot.com
Source: https://www.techtalk7.com/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue/
I can not confirm, that the selected solution from nosklo on 16:52 is working. For me draw does not open a window to display the plot, only the blocking show at the end displays the solution. However, his reply from 17:00 is correct. Switching interactive mode on via
ion()
fixes the problem.Reply ↓
if you are an advanced programmer, you can use
os.fork()
but keep in mind that usingos.fork()
can be tricky because you are creating a new process by copying the old process.Reply ↓
IMPORTANT: Just to make something clear. I assume that the commands are inside a
.py
script and the script is called using e.g.python script.py
from the console.A simple way that works for me is:
Example of
script.py
file:Reply ↓
Reply ↓
I had to also add
plt.pause(0.001)
to my code to really make it working inside a for loop (otherwise it would only show the first and last plot):Reply ↓
In my opinion, the answers in this thread provide methods which don't work for every systems and in more complex situations like animations. I suggest to have a look at the answer of MiKTeX in the following thread, where a robust method has been found:
How to wait until matplotlib animation ends?
Reply ↓
If you are working in console, i.e.
IPython
you could useplt.show(block=False)
as pointed out in the other answers. But if you're lazy you could just type:Which will be the same.
Reply ↓
I also wanted my plots to display run the rest of the code (and then keep on displaying) even if there is an error (I sometimes use plots for debugging). I coded up this little hack so that any plots inside this
with
statement behave as such.This is probably a bit too non-standard and not advisable for production code. There is probably a lot of hidden "gotchas" in this code.
If/when I implement a proper "keep the plots open (even if an error occurs) and allow new plots to be shown", I would want the script to properly exit if no user interference tells it otherwise (for batch execution purposes).
I may use something like a time-out-question "End of script! \nPress p if you want the plotting output to be paused (you have 5 seconds): " from https://stackoverflow.com/questions/26704840/corner-cases-for-my-wait-for-user-input-interruption-implementation.
Reply ↓
If you want to open multiple figures, while keeping them all opened, this code worked for me:
Reply ↓
My answer is a little off topic, but I just decided to get away from matplotlib in my application because of the problem that is stated in this question.
Alternatively, you can use plotly. It does not block the process.
Reply ↓
While not directly answering OPs request, Im posting this workaround since it may help somebody in this situation:
for this Im using:
Where "var" identifies the plot in the loop so it wont be overwritten.
Reply ↓
The OP asks about detatching
matplotlib
plots. Most answers assume command execution from within a python interpreter. The use-case presented here is my preference for testing code in a terminal (e.g. bash) where afile.py
is run and you want the plot(s) to come up but the python script to complete and return to a command prompt.This stand-alone file uses
multiprocessing
to launch a separate process for plotting data withmatplotlib
. The main thread exits using theos._exit(1)
mentioned in this post. Theos._exit()
forces main to exit but leaves thematplotlib
child process alive and responsive until the plot window is closed. It's a separate process entirely.This approach is a bit like a Matlab development session with figure windows that come up with a responsive command prompt. With this approach, you have lost all contact with the figure window process, but, that's ok for development and debugging. Just close the window and keep testing.
multiprocessing
is designed for python-only code execution which makes it perhaps better suited thansubprocess
.multiprocessing
is cross-platform so this should work well in Windows or Mac with little or no adjustment. There is no need to check the underlying operating system. This was tested on linux, Ubuntu 18.04LTS.Running
file.py
brings up a figure window, then__main__
exits but themultiprocessing
+matplotlib
figure window remains responsive with zoom, pan, and other buttons because it is an independent process.Check the processes at the bash command prompt with:
ps ax|grep -v grep |grep file.py
Reply ↓
Use
plt.show(block=False)
, and at the end of your script callplt.show()
.This will ensure that the window won't be closed when the script is finished.
Reply ↓
It is better to always check with the library you are using if it supports usage in a non-blocking way.
But if you want a more generic solution, or if there is no other way, you can run anything that blocks in a separated process by using the
multprocessing
module included in python. Computation will continue:That has the overhead of launching a new process, and is sometimes harder to debug on complex scenarios, so I'd prefer the other solution (using
matplotlib
's nonblocking API calls)Reply ↓
Use
matplotlib
's calls that won't block:Using
draw()
:Using interactive mode:
Reply ↓
You may want to read this document in
matplotlib
's documentation, titled:Using matplotlib in a python shell
Reply ↓
In my case, I wanted to have several windows pop up as they are being computed. For reference, this is the way:
PS. A quite useful guide to matplotlib's OO interface.
Reply ↓
In many cases it is more convenient til save the image as a .png file on the hard drive. Here is why:
Advantages:
time.
Drawback:
Reply ↓
Use the keyword 'block' to override the blocking behavior, e.g.
to continue your code.
Reply ↓
Try
The
show()
documentation says:Reply ↓
On my system show() does not block, although I wanted the script to wait for the user to interact with the graph (and collect data using 'pick_event' callbacks) before continuing.
In order to block execution until the plot window is closed, I used the following:
Note, however, that canvas.start_event_loop_default() produced the following warning:
although the script still ran.
Reply ↓
Well, I had great trouble figuring out the non-blocking commands… But finally, I managed to rework the "Cookbook/Matplotlib/Animations – Animating selected plot elements" example, so it works with threads (and passes data between threads either via global variables, or through a multiprocess
Pipe
) on Python 2.6.5 on Ubuntu 10.04.The script can be found here: Animating_selected_plot_elements-thread.py – otherwise pasted below (with fewer comments) for reference:
Hope this helps someone,
Cheers!
Reply ↓
Good Afternoon everyone ! can anyone recommend where I can buy Pachamama CBD Softgels Full Spectrum Vegan Capsules 25mg?
Reply ↓
Does anyone know what happened to Dimepiece LA celebrity streetwear brand? I cannot check out on Dimepiecela site. I've read in Vogue that they were acquired by a UK-based hedge fund in excess of $50 million. I have just bought the Meditate Yoga Bag from Ebay and totally love it xox
Reply ↓
how much cbd vape is too much
Reply ↓