![]() #!/usr/bin/python3įrom matplotlib.pyplot import plot, draw, show This was tested on linux, Ubuntu 18.04LTS. There is no need to check the underlying operating system. multiprocessing is cross-platform so this should work well in Windows or Mac with little or no adjustment. Multiprocessing is designed for python-only code execution which makes it perhaps better suited than subprocess. With this approach, you have lost all contact with the figure window process, but, that's ok for development and debugging. This approach is a bit like a Matlab development session with figure windows that come up with a responsive command prompt. The os._exit() forces main to exit but leaves the matplotlib child process alive and responsive until the plot window is closed. The main thread exits using the os._exit(1) mentioned in this post. This stand-alone file uses multiprocessing to launch a separate process for plotting data with matplotlib. bash) where a file.py is run and you want the plot(s) to come up but the python script to complete and return to a command prompt. The use-case presented here is my preference for testing code in a terminal (e.g. Most answers assume command execution from within a python interpreter. The OP asks about detatching matplotlib plots. ![]() ![]() 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. 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). Print "Error early edition while waiting for show():" If keep_show_open_on_exit and even_when_error: To continue excecuting code when plt.show() is calledĪnd keep the plot on displaying before this contex manager exits from contextlib import keep_plots_open(keep_show_open_on_exit=True, even_when_error=True): There is probably a lot of hidden "gotchas" in this code. This is probably a bit too non-standard and not advisable for production code. I coded up this little hack so that any plots inside this with statement behave as such. 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). Print "out" # will never print - show() blocks indefinitely! T0 = threading.Thread(target=threadMainTest) Print 'FPS:', update_line.cnt/(time.time()-tstart) If pipe1upd.poll(): # check first if there is anything to receive If mypass > 100: # start "speeding up" animation, only after 100 counts have passed # parallel with gobject.idle_add(update_line) # will now be done in a "main thread" - so it can run in # the kind of processing we might want to do in a main() function, # just a plain global var to pass data (from main, to plot update thread) # is drawn and saved in the pixel buffer backgroundīackground = py_from_bbox(ax.bbox) # save the clean slate background - everything but the animated line Line, = ax.plot(x, nx.sin(x), animated=True) The script can be found here: Animating_selected_plot_elements-thread.py - otherwise pasted below ( with fewer comments) for reference: import sys 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. Well, I had great trouble figuring out the non-blocking commands.
0 Comments
Leave a Reply. |