1
\$\begingroup\$

I have a stream of sensor data which I want to visualize in a plot with many subplots. Plotting the data is a real bottleneck in my code. Right now I get with small resolution only 16 FPS which is far too slow.

Here is what it looks like:

enter image description here

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

def live_plot():
    loops=100
    n = 7
    p = 30
    fig, axes = plt.subplots(ncols=n, nrows=n)
    fig.canvas.draw()     
    handles=[]
    axes = np.array(axes)
    for ax in axes.reshape(-1):
        ax.axis("off")
        handles.append(ax.imshow(np.random.rand(p,p), interpolation="None", cmap="RdBu"))  
    t0 = time.time()
    for i in np.arange(loops):
        for h in handles: h.set_data(np.random.rand(p,p)) 
        for h, ax in zip(handles, axes.reshape(-1)): ax.draw_artist(h)
        plt.pause(1e-12)
    print("avg: " + "%.1f" % (loops/(time.time()-t0)) + " FPS")

live_plot()

What can I do to get more frames per second?

\$\endgroup\$
  • \$\begingroup\$ You would probably get better answers if you included some sample data in your question. \$\endgroup\$ – 200_success Oct 28 '18 at 12:23
  • \$\begingroup\$ @200_success My sensors stream at 120 FPS. Random numbers from numpy are fine to simulate sensor data and real data are probably not essential to make the code run faster. Please tell me if I am wrong. \$\endgroup\$ – Samuel Oct 28 '18 at 12:30
  • \$\begingroup\$ Isn't 120 FPS way too fast to actually notice if anything meaningful changed in some frame? In other words, wouldn't it make it better to collate data for, say, one second and display that? \$\endgroup\$ – Graipher Oct 29 '18 at 11:26
  • \$\begingroup\$ @Graipher In low resolution my sensors steam at 120 FPS. At higher resolution the frame rate drops quickly and my code is even slower. \$\endgroup\$ – Samuel Oct 29 '18 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.