Skip to content

Instantly share code, notes, and snippets.

@reox
Forked from electronut/ldr.py
Last active August 29, 2015 14:13
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 reox/b9b6c8d8d266fc2b019c to your computer and use it in GitHub Desktop.
Save reox/b9b6c8d8d266fc2b019c to your computer and use it in GitHub Desktop.
"""
Display Random Data using matplotlib's animation function
forked from:
Author: Mahesh Venkitachalam
Website: electronut.in
"""
import sys
import numpy as np
from time import sleep
from collections import deque
import random
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# plot class
class AnalogPlot:
# constr
def __init__(self, maxLen):
self.ax = deque([0.0]*maxLen)
self.maxLen = maxLen
# add to buffer
def addToBuf(self, buf, val):
if len(buf) < self.maxLen:
buf.appendleft(val)
else:
buf.popleft()
buf.append(val)
# add data
def add(self, data):
self.addToBuf(self.ax, data)
# update plot
def update(self, frameNum, a0):
try:
self.add(random.random()*1023)
a0.set_data(range(self.maxLen), self.ax)
except KeyboardInterrupt:
print('exiting')
return a0,
# main() function
def main():
# plot parameters
maxLen = 1000
analogPlot = AnalogPlot(maxLen)
print('plotting data...')
# set up animation
fig = plt.figure()
ax = plt.axes(xlim=(0, maxLen), ylim=(0, 1023))
a0, = ax.plot([], [])
anim = animation.FuncAnimation(fig, analogPlot.update,
fargs=(a0,),
interval=50)
# show plot
plt.show()
print('exiting.')
# call main
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment