Created
October 27, 2018 16:29
-
-
Save skyzh/1c519637dda8c9fb0f097f8429b566c2 to your computer and use it in GitHub Desktop.
Live Plot
This file contains 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 matplotlib.pyplot as plt | |
from matplotlib import animation | |
from numpy import random | |
LINE_NUM = 6 | |
DATA_NUM = 100 | |
LABEL=["ax","ay","az","gx","gy","gz"] | |
fig = plt.figure() | |
ax = plt.axes(xlim=(0, DATA_NUM), ylim=(-5000, 5000)) | |
line, = ax.plot([], [], lw=2) | |
lines = [] | |
for index in range(LINE_NUM): | |
lobj, = ax.plot([],[],lw=2, label=LABEL[index]) | |
lines.append(lobj) | |
ax.legend() | |
def init(): | |
for line in lines: | |
line.set_data([],[]) | |
return lines | |
all_data = [[0] * LINE_NUM] | |
def pad(data, num): | |
pad_cnt = num - len(data) | |
if pad_cnt > 0: | |
return [0] * pad_cnt + data | |
else: | |
return data[-num:] | |
def animate(i): | |
try: | |
_i = (input().split(','))[0:-1] | |
data = list(map(lambda x: int(x), _i)) | |
if len(data) == LINE_NUM: | |
all_data.append(data) | |
except: | |
pass | |
xlist = [list(range(DATA_NUM))] * LINE_NUM | |
ylist = list(map(list, zip(*all_data))) | |
for lnum,line in enumerate(lines): | |
line.set_data(xlist[lnum], pad(ylist[lnum], DATA_NUM)) # set data for each line separately. | |
line.set_label(LABEL[lnum]) | |
return lines | |
# call the animator. blit=True means only re-draw the parts that have changed. | |
anim = animation.FuncAnimation(fig, animate, init_func=init, interval=20, blit=True) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment