Skip to content

Instantly share code, notes, and snippets.

@rudifa
Created July 7, 2016 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudifa/05ac3bd00e1ce6bcd6021ca9b58c380e to your computer and use it in GitHub Desktop.
Save rudifa/05ac3bd00e1ce6bcd6021ca9b58c380e to your computer and use it in GitHub Desktop.
scope.py emulates an oscilloscope
# scope.py v 0.1 by Rudi Farkas
# ref http://matplotlib.org/examples/animation/
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class Scope():
def __init__(self, xy_data_gen, interval_ms=100):
self.xy_data_gen = xy_data_gen # data generator, must yield (x, y) np.arrays
self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot([])
self.limits_set = False
ani = animation.FuncAnimation(self.fig, self.update_plot, xy_data_gen, interval=interval_ms)
plt.show()
def update_plot(self, data):
x, y = data
if not self.limits_set:
self.ax.set_xlim(np.min(x), np.max(x))
self.ax.set_ylim(np.min(y), np.max(y))
self.limits_set = True
self.line.set_xdata(x)
self.line.set_ydata(y)
return self.line,
if __name__ == '__main__':
def demo_xy_data():
data_len = 200
x = np.linspace(0, 4*np.pi, data_len, endpoint=True)
s = np.sin(x)
while True:
yield x, s + 0.1 * np.random.rand(data_len)
scope = Scope(demo_xy_data, 200)
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment