Matplotlib python show() returns immediately

PythonMatplotlib

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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()

Solution 2 - Python

I think that using show(block=True) should fix your problem.

Solution 3 - Python

Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)

This worked for me:

import matplotlib
matplotlib.interactive(False)

(Actually, I wanted interactive mode, but in your case the inverse should help.) ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.

This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here

Solution 4 - Python

To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:

import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()

Solution 5 - Python

I had the same problem with this code below.

import matplotlib.pyplot as plt

plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))

plt.show()

I removed plt.ion(), and the plot stays without closing automatically.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongpierrisView Question on Stackoverflow
Solution 1 - PythonCraigTeegardenView Answer on Stackoverflow
Solution 2 - PythonGuy AdiniView Answer on Stackoverflow
Solution 3 - PythonZakView Answer on Stackoverflow
Solution 4 - Pythonbio_jamesView Answer on Stackoverflow
Solution 5 - PythonAbhijith H RView Answer on Stackoverflow