plt.show() - what does it do under the hood?

I have a number of questions, but I’ll start with one. Consider the following code:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 6)
plt.plot(x, x*1.5)
plt.show()
s = raw_input(‘Enter anything to continue:’)
plt.show()
print ‘The end’

Question: The first plt.show() launches an interactive window. Control returns to the script upon its close. The second plt.show() does nothing that I can see. Why?

Ubuntu 11.04, Python 2.7.1, Matplotlib 0.99.3

That is because closing the figure destroys that object. If you want full interactive mode, I suggest updating to v1.0.1 (or the upcoming v1.1.0 release) and use plt.ion(). The show() will trigger the figures to appear, but control immediately returns to the script.

Interactivity in earlier versions of mpl is a bit more hit-or-miss, so you can try it out first before updating. Also note that multiple calls to plt.show() in earlier versions are not recommended, but are allowed in v1.0 and up.

I hope this helps,
Ben Root

···

On Mon, Aug 29, 2011 at 10:26 PM, Trevor J Christensen <trevor@…3746…> wrote:

I have a number of questions, but I’ll start with one. Consider the following code:

import matplotlib as mpl
import matplotlib.pyplot as plt

import numpy as np
x = np.arange(1, 6)
plt.plot(x, x*1.5)
plt.show()
s = raw_input(‘Enter anything to continue:’)
plt.show()
print ‘The end’

Question: The first plt.show() launches an interactive window. Control returns to the script upon its close. The second plt.show() does nothing that I can see. Why?

Ubuntu 11.04, Python 2.7.1, Matplotlib 0.99.3