Last active
February 28, 2020 01:52
-
-
Save Seanny123/85239f6f53c980128d3585595d734fc3 to your computer and use it in GitHub Desktop.
Extremely simple continuously animated line using Python and matplotlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from collections import deque | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
class AnalogPlot: | |
def __init__(self, data, display_len): | |
self.buff = deque([0.0]*display_len) | |
self.display_len = display_len | |
self.data = data | |
def add_to_buff(self, buf, val): | |
if len(buf) < self.display_len: | |
buf.append(val) | |
else: | |
buf.pop() | |
buf.appendleft(val) | |
def update(self, frame, a0, ax): | |
self.add_to_buff(self.buff, self.data[frame:frame+1]) | |
a0[0].set_data(range(self.display_len), self.buff) | |
ax.set_xticklabels((str(frame), str(frame+self.display_len))) | |
return a0 | |
def main(): | |
t_max = 100 | |
sin_sig = np.sin(np.linspace(0, 20*np.pi, 1000)) | |
analog_plot = AnalogPlot(sin_sig, t_max) | |
# set up animation | |
fig = plt.figure() | |
ax = fig.add_subplot(111, xlim=(0, t_max), ylim=(-1, 1)) | |
ax.set_xticks((0,t_max)) | |
a0 = ax.plot([], []) | |
anim = animation.FuncAnimation(fig, analog_plot.update, | |
fargs=(a0, ax), | |
interval=1.0) | |
plt.show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment