Fix Python – How do I plot in real-time in a while loop using matplotlib?

I am trying to plot some data from a camera in real time using OpenCV. However, the real-time plotting (using matplotlib) doesn’t seem to be working.
I’ve isolated the problem into this simple example:
fig = plt.figure()
plt.axis([0, 1000, 0, 1])

i = 0
x = list()
y = list()

while i < 1000: temp_y = np.random.random() x.append(i) y.ap....