Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib python show() returns immediately

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.

The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.

Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?

EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.

like image 603
gpierris Avatar asked Feb 14 '12 16:02

gpierris


People also ask

Is PLT show () necessary?

Plotting from an IPython shell Some changes (such as modifying properties of lines that are already drawn) will not draw automatically: to force an update, use plt. draw() . Using plt. show() in Matplotlib mode is not required.

Is PLT show () blocking?

Answer #6: plt. show() and plt. draw() are unnecessary and / or blocking in one way or the other.

How can you avoid the show PLT?

Plot a line using plot() method. Display the figure using Show() method. To display the figure, use Show() method with block=False.

When should I call the PLT show?

In interactive tools, You need to use plt. show() when all your plots are created.


1 Answers

I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.

Incorrect code. Causes the graph to flash on screen for a brief instant:

    import matplotlib.pyplot as plt      x = [1,2,3]     y = [5,6,7]      fig = plt.figure()     plt.plot(x, y)      fig.show() 

Last line should be as follows to show the graph until it is dismissed:

    plt.show() 
like image 191
CraigTeegarden Avatar answered Sep 21 '22 13:09

CraigTeegarden