Skip to content

Instantly share code, notes, and snippets.

@Uberi
Created April 25, 2015 02:10
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Uberi/283a13b8a71a46fb4dc8 to your computer and use it in GitHub Desktop.
Save Uberi/283a13b8a71a46fb4dc8 to your computer and use it in GitHub Desktop.
Realtime plotting with Matplotlib.
#!/usr/bin/env python3
import time, random
import math
from collections import deque
start = time.time()
class RealtimePlot:
def __init__(self, axes, max_entries = 100):
self.axis_x = deque(maxlen=max_entries)
self.axis_y = deque(maxlen=max_entries)
self.axes = axes
self.max_entries = max_entries
self.lineplot, = axes.plot([], [], "ro-")
self.axes.set_autoscaley_on(True)
def add(self, x, y):
self.axis_x.append(x)
self.axis_y.append(y)
self.lineplot.set_data(self.axis_x, self.axis_y)
self.axes.set_xlim(self.axis_x[0], self.axis_x[-1] + 1e-15)
self.axes.relim(); self.axes.autoscale_view() # rescale the y-axis
def animate(self, figure, callback, interval = 50):
import matplotlib.animation as animation
def wrapper(frame_index):
self.add(*callback(frame_index))
self.axes.relim(); self.axes.autoscale_view() # rescale the y-axis
return self.lineplot
animation.FuncAnimation(figure, wrapper, interval=interval)
def main():
from matplotlib import pyplot as plt
fig, axes = plt.subplots()
display = RealtimePlot(axes)
display.animate(fig, lambda frame_index: (time.time() - start, random.random() * 100))
plt.show()
fig, axes = plt.subplots()
display = RealtimePlot(axes)
while True:
display.add(time.time() - start, random.random() * 100)
plt.pause(0.001)
if __name__ == "__main__": main()
@dandaveacosta
Copy link

sir, is there a way not to use plt.pause()?

@Uberi
Copy link
Author

Uberi commented Nov 24, 2019

Hi @dandave1114, this StackOverflow answer has a few alternatives for animation, one of which is using plt.pause, but there are multiple other options there as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment