0

I'm trying to display the frames captured by a CCD camera in real-time using pyplot animation. I wrote a short python script just to test this out, and while it works, it does so erratically. It will quickly update a dozen or so animation frames, then pause for a second, then update again, then pause again, and so on. I'd like to just have it update the plot continuously and smoothly, but I'm not sure where I'm going wrong.

I know it isn't the part where it makes a call to the camera's frame buffer; I tested out just calling that in a loop and it never slowed down, so I think it's somewhere in the actual processing of the animation frames.

My code is below:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import Pixis100
import time

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)    

# Create ccd object
ccd = Pixis100.device()
ccd.snapshot()
ccd.focusStart()
# focusStart() tells the camera to start putting captured frames into the buffer

line, = ax.plot(ccd.getFrame()[:,2:].sum(axis=0)[::-1],'b-')
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame

# animation function
def update(data):    
    line.set_ydata(data)
    return line,

def data_gen():
    while True: yield ccd.getFrame()[:,2:].sum(axis=0)[::-1]

# call the animator
anim = animation.FuncAnimation(fig, update, data_gen,interval=10,blit=True)

plt.show()
ccd.focusStop()
# focusStop() just tells the camera to stop capturing frames

For reference, ccd.getFrame()[:,2:].sum(axis=0)[::-1] returns a 1x1338 array of integers. I wouldn't think this would be too much for the animation to handle at one time.

2
  • does it run better if you increase the interval? I would point out 10ms is 100fps. I suspect the culprit here is the gui-frame work you are using not being able to re-draw the canvas at that rate. It as also not clear if you have a question or are just whining. Commented Jul 13, 2013 at 15:43
  • Changing the interval doesn't help. My question is what would cause the plot to update erratically, as I described; if it's an issue of pyplot and a different plotting library would work better, or if there's something within pyplot I can do to fix this issue. Commented Jul 14, 2013 at 17:37

1 Answer 1

1

The problem is not in animation, the following works just fine:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import time

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)    
ax.set_xlim([0, 2 *np.pi])
ax.set_ylim([-1, 1])

th = np.linspace(0, 2 * np.pi, 1000)

line, = ax.plot([],[],'b-', animated=True)
line.set_xdata(th)
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame

# animation function
def update(data):    
    line.set_ydata(data)

    return line,

def data_gen():
    t = 0
    while True:
        t +=1
        yield np.sin(th + t * np.pi/100)

# call the animator
anim = animation.FuncAnimation(fig, update, data_gen, interval=10, blit=True)
plt.show()

The choppyness may come from either your frame grabber, the computation your are doing on it, or issues with the gui getting enough time on the main thread to re-draw. See time.sleep() required to keep QThread responsive?

Sign up to request clarification or add additional context in comments.

1 Comment

The graph not animating on my Jupyter notbook. Anything need to configure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.