First, plt.show() gets called once (see here on Stackoverflow and here on the Matplotlib mailing list). That's simply how Matplotlib is intended to be used. You prepare your plots and then plt.show() is the last line in the script.
But what if we want to show and interact with more than one plot? The trick is to prepare the plots ahead of time and still call plt.show() once at the end. Here's an example that uses the onclick event like in your code. It cycles through a few plots and then stops at the end. You'll have to rearrange your code a bit to save the results of whatever is going on in the for loop.
import numpy as np
import matplotlib.pyplot as plt
# prepare some pretty plots
stuff_to_plot = []
for i in range(10):
stuff_to_plot.append(np.random.rand(10))
fig = plt.figure()
ax = fig.add_subplot(111)
coord_index = 0
plot_index = 0
def onclick(event):
# get the index variables at global scope
global coord_index
if coord_index != 6:
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
coord_index += 1
else:
coord_index = 0
global plot_index
# if we have more to plot, clear the plot and plot something new
if plot_index < len(stuff_to_plot):
plt.cla()
ax.plot(stuff_to_plot[plot_index])
plt.draw()
plot_index += 1
cid = fig.canvas.mpl_connect('button_press_event', onclick)
# plot something before fist click
ax.plot(stuff_to_plot[plot_index])
plot_index += 1
plt.show()