Skip to content

Instantly share code, notes, and snippets.

@skyzh
Created October 27, 2018 16:29
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 skyzh/1c519637dda8c9fb0f097f8429b566c2 to your computer and use it in GitHub Desktop.
Save skyzh/1c519637dda8c9fb0f097f8429b566c2 to your computer and use it in GitHub Desktop.
Live Plot
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