I have a program with rapid animations which works perfectly under pygame, and for technical reasons, I need to do the same using only matplotlib or an other widespread module.
The program structure is roughly:
pygame.init()
SURF = pygame.display.set_mode((500, 500))
arr = pygame.surfarray.pixels2d(SURF) # a view for numpy, as a 2D array
while ok:
# modify some pixels of arr
pygame.display.flip()
pygame.quit()
I have no low level matplotlib experience, but I think it is possible to do equivalent things with matplotlib. In other words :
How to share the bitmap of a figure, modify some pixels and refresh the screen ?
Here is a minimal working exemple, which flips 250 frames per second (more than the screen ...) on my computer :
import pygame,numpy,time
pygame.init()
size=(400,400)
SURF = pygame.display.set_mode(size)
arr = pygame.surfarray.pixels2d(SURF) # buffer pour numpy
t0=time.clock()
for counter in range(1000):
arr[:]=numpy.random.randint(0,0xfffff,size)
pygame.display.flip()
pygame.quit()
print(counter/(time.clock()-t0))
EDIT
What I try with indications in answers :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 400)
y = np.linspace(0, 2 * np.pi, 400).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
count=0
t0=time.clock()+1
def updatefig(*args):
global x, y,count,t0
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
if time.clock()<t0:
count+=1
else:
print (count)
count=0
t0=time.clock()+1
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()
But this only provides 20 fps....
It should be noted that the human brain is capable of "seeing" up to a framerate of ~25 fps. Faster updates are not actually resolved.
Matplotlib
With matplotlib and its
animation
module the example from the question runs with 84 fps on my computer.PyQtGraph
In pyqtgraph a higher framerate is obtained, it would run with 295 fps on my computer.
If you just need to animate a
matplotlib
canvas the animation framework is the answer. There's a simple example here that does basically what you ask.If this is going to be part of a more complex application you probably want finer control over a specific backend.
Here's a quick attempt using
Qt
loosely based on this matplotlib example.It's using a
QTimer
for the updates, probably there's also some idle callback inQt
you could attach to.One thing you should be careful with is that
imshow
computes the image normalization on the first frame. In the subsequent frames it's callingset_data
so the normalization stays the same. If you want to update it you can callimshow
instead (probably slower). Or you could just fix it manually withvmin
andvmax
in the firstimshow
call and provide properly normalized frames.If you want to animate a plot, then you can take a look at the animation functionality in matplotlib under
matplotlib.animation.Animation
. Here's a great tutorial - https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial.If you just want to periodically update an adhoc bitmap, I am not sure matplotlib is meant for what you are trying to achieve. From matplotlib docs:
If you would like to periodically update an adhoc image on the screen, you may want to look into GUI libraries for python. Here is a short summary of available options - https://docs.python.org/3/faq/gui.html. Tkinter is a pretty standard one and is shipped with python. You can use the
ImageTk
module inpillow
to create/modify images for displaying via Tkinter - http://pillow.readthedocs.io/en/4.2.x/reference/ImageTk.html.Given you talked about using widespread modules, here's a proof of concept using
OpenCV
. It runs pretty fast here, up to 250-300 generated frames per second. It's nothing too fancy, just to show that maybe if you're not using any plotting featurematplotlib
shouldn't really be your first choice.