0

There isn't duplicated answer because I was always blocked by solution below !!!!!!!

I want to plot a graph and still run following code without closing the graph automatically like Matlab.

I try plt.show(block=False) , it failed and appear in a small moment then close itself.

Code:

import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    plt.figure(figsize=(10, 10))
    plt.plot(range(5), lw=2, label='Real')
    plt.title('Prediction')
    plt.legend(loc="best")
    plt.show(block=False)
    print("---Plot graph finish---")

I try plt.draw() or interactive mode , it failed, too. Check the code below.

Code:

import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    plt.figure(figsize=(10, 10))
    plt.plot(range(5), lw=2, label='Real')
    plt.title('Prediction')
    plt.legend(loc="best")
    plt.draw()
    plt.show()
    print("---Plot graph finish---")

Above will block until I close it.

Code:

import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    plt.ion()
    plt.figure(figsize=(10, 10))
    plt.plot(range(5), lw=2, label='Real')
    plt.title('Prediction')
    plt.legend(loc="best")
    plt.draw()
    plt.show()
    print("---Plot graph finish---")

Above will appear nothing, or it appear and disappear very fast.

My version is below:

user@ya:~/$ sudo pip freeze | grep matplotlib
matplotlib==2.2.3
user@ya:~/$ sudo pip -V
pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

Environments:

I only execute script in Ubuntu ex: user@ya: python xxx.py
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:   xenial

Can anyone help me? I just want to do like Matlab which won't close the plotted graph even if the script finishes.

code_worker
  • 384
  • 3
  • 16
  • @Mr.T I add the env info, but I always run it on Ubuntu. – code_worker Nov 01 '18 at 08:57
  • @DavidG , No duplicate, because I do it but failed....I have no idea how they achieve that. – code_worker Nov 01 '18 at 10:19
  • I would be tempted to close this as duplicate as well. The answers over there show precisely how to not block the code execution on showing a figure and the question here does not make it apparent where the difference is. – ImportanceOfBeingErnest Nov 01 '18 at 12:17
  • @code_worker The only thing that works in my environment is [plt.draw() with plt.pause() such as in this example](https://stackoverflow.com/a/33050617/8881141) or [in this example](https://stackoverflow.com/a/40509088/8881141). – Mr. T Nov 01 '18 at 15:30
  • @Mr.T , when your script finish, I guess the graph be closed. But I want the graph kept even the script finished. – code_worker Nov 02 '18 at 01:48
  • Have you tried these examples? In my environment the figures are opened and updated with `plt.draw/plt.pause` and stay open after the script finishes because of the final `plt.plot`. – Mr. T Nov 02 '18 at 07:13
  • @Mr.T , I tried it, and graph shows 0.01sec(10 * 0.001) and close automatically after script finishes. Maybe keeing graph after script finishing only Matlab can do that. – code_worker Nov 02 '18 at 08:42
  • Obviously not, because I just wrote that in my environment these two scripts keep the figures open after the script finishes. – Mr. T Nov 02 '18 at 09:13
  • @Mr.T , May I ask your version of matplotlib? maybe the problem of version? Or the environment. Many people test successfully through IPython, but I only can run it in Ubuntu through command line. – code_worker Nov 02 '18 at 09:39
  • Probably not matplotlib version specific (mine 2.2.3 on Win or 3.0.0 on Ubuntu wiht Eclipse/PyDev) rather that you run it on command line. I would delete this question and ask another one specifically about non-blocking behaviour from the command line. Wouldn't be surprised, if the command line closes the window after finishing the script, but I don't know much about it. Maybe even rather a question for [askubuntu](https://askubuntu.com/)? – Mr. T Nov 02 '18 at 09:53
  • @Mr.T , Ok I should ask another question. thx – code_worker Nov 02 '18 at 10:13

1 Answers1

0

The plots will close when the script is finished, even if you use block=False

Similar to the answer here, when running from the terminal you have to call plt.show at the end of the script to keep these plots open after you've finished. I made a similar code to yours which works as intended; the plot simply refreshes at the end of the code. (added a for loop for a 5 second delay just so you can see it's running).

import numpy as np
import matplotlib.pyplot as plt
import time


if __name__ == '__main__':
    plt.figure(figsize=(10, 10))
    plt.plot(range(5), lw=2, label='Real')
    plt.title('Prediction')
    plt.legend(loc="best")
    plt.show(block=False)
    print("---Plot graph finish---")
    for i in range(5):
        print('waiting...{}'.format(i))
        time.sleep(1)
    print('code is done')
    plt.show()
delfd1
  • 16