Matplotlib python show() returns immediately

Solution 1:

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:

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

Solution 3:

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:

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:

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.