0

I have been trying to figure out how to make a real time updating graph using Matplotlib, without using FuncAnimation() because it was too slow for my final purpose. I read through This Article, which explains how to speed up the plotting in Matplotlib. I then tried to implement his method with a real time graph method, but unfortuanately I still have only basic Python/Matplotlib skills to do such a thing.

I was able to get as far as creating an x and y limit and creating a blank line which I would like to update in a while loop with random numbers for now. I am able to create random numbers every loop but I am unable to update the numbers to the graph. I do not want the x values to change but rather the y values to change while being updated. Something that looks like a heart monitor would for example. Here is my following code up to where I am stuck:

import matplotlib.pyplot as plt
import random

x_len = 100
y_range = [-1,10]
xs = list(range(0,100))
ys = [0] * x_len

fig, ax = plt.subplots()
line, = ax.plot(xs,ys)
ax.set_ylim(y_range)
plt.show(block=False)
fig.canvas.draw()


while True:
    y = random.randint(-10,10)
    line.set_ydata(ys)
    ax.draw_artist(ax.patch)
    ax.draw_artist(line)
    fig.canvas.flush_events()

    print("one loop")
    print(y)
  • FuncAnimation is not slower than any other method you can use. It could be that matplotlib itself is too slow for your purpose though. – ImportanceOfBeingErnest May 02 '19 at 12:16
  • Possibly, but everyone keeps directing me to to the first article in my main post. Might as well give it a try before I move on. – sentientnativeplant May 02 '19 at 12:24
  • 1
    Let me direct you to [this answer](https://stackoverflow.com/a/50390093/4124317) which shows how to use FuncAnimation and get a framerate above 80 for an image. Now a single line is of course a lot less data to update. So I changed that example to your case of a line with 100 points and it runs with 250 fps. That is 10 times faster than the human brain can resolve, hence it should be sufficient. – ImportanceOfBeingErnest May 02 '19 at 13:05
  • Thank you, I will attempt to use FuncAnimation again and apply this to my work. – sentientnativeplant May 02 '19 at 15:36
  • The `blit=True` is kind of the trick here if the axes don't change. axes creation is kind of slow. – Jody Klymak May 02 '19 at 16:24

0 Answers0