6

Is it possible to run a Python code which generates a figure (or figures) and keep these figures open, even after the program ends?

Currently, when I use show() at the end of my code, the program execution $ python somecode.py halts until I close the figure (or figures). Thereby making the terminal unusable.

Do I need to some how assign a different job id for each figure, so that the figures are running separately from the main program ? If so how can I accomplish that ?

This need becomes apparent when one wants to execute a program limited amount of times, with different input, and without wanting to store each resulting figure as a (separate) file. This is handy for making quick comparisons.

An obvious solution is to run the program in separate terminal sessions.

DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
imranal
  • 656
  • 12
  • 35
  • If you already know the obvious solution, why not use it? – cel Oct 24 '15 at 14:17
  • 1
    It seems like over kill to open another terminal. There has to be a simpler solution for this right ? One problem with opening another terminal is that you have to do this every time you want to run another simulation. Which simply causes clutter. Instead of just having multiple figures from different simulations open, you have in addition several terminals. – imranal Oct 24 '15 at 14:21
  • 1
    @cel, because often better solutions exist. [As constructive responses will illustrate](http://stackoverflow.com/help/behavior) – DilithiumMatrix Oct 24 '15 at 14:51
  • @DilithiumMatrix, thanks for pointing me to this interesting read. – cel Oct 24 '15 at 15:38

2 Answers2

3

Not sure if it is what you are after, but you can of course start python as a background process in your terminal using &.

Like this:

$ python somecode.py &
$ ...         # control is returned here immediately

It will start python in the background and return control to the terminal immediately. After a while the plot window will appear without blocking the terminal. You can fire multiple instances of python this way, resulting in multiple plot windows being open at the same time while keeping the terminal usable.

Note that you can also move a process to the background after it was started. After

$ python somecode.py

the terminal is blocked, but you can press ctrl-z in the terminal followed by bg to turn the process in a background process.

This is of course general shell functionality, not related to Python.

WhiteViking
  • 3,146
  • 1
  • 19
  • 22
  • Of course! This is exactly what I needed. Though I must say, there is a disadvantage when I do this. I simply want to let the figures exist after the program finishes executing. However, since I am running the program (which is still in the background) that eats up a lot of memory. For just displaying some figures - in my case it is 80MB per python execution. – imranal Oct 25 '15 at 12:25
2

For the image to continue being displayed, python must still be running. If python is still running, then in standard usage you won't be able to use the terminal.

In general, with this type of workflow one might use ipython, where you can easily run the desired program/code/script, then keep using the interactive interpreter.

Alternative solutions include:
- Moving the running process to the background
- Running the process in the background
- Running the process in gnu screen.

Community
  • 1
  • 1
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119