Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
testit.py
1 import numpy as np
2 import matplotlib.pyplot as plt
3 import matplotlib.lines as ln
4 import matplotlib.animation as animation
5 
6 example = 4
7 
8 if example == 1:
9  def update_line(num, data, line):
10  #print 'dbg', num, data, line
11  line.set_data(data[...,:num])
12  return line,
13 
14  fig1 = plt.figure()
15 
16  data = np.random.rand(2, 25)
17  #print 'dbg', data
18  l, = plt.plot([], [], 'r-')
19  plt.xlim(0, 1)
20  plt.ylim(0, 1)
21  plt.xlabel('x')
22  plt.title('test')
23  line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
24  interval=1000, blit=True)
25  #line_ani.save('lines.mp4')
26 
27 
28 elif example == 2:
29  fig2 = plt.figure()
30 
31  x = np.arange(-9, 10)
32  y = np.arange(-9, 10).reshape(-1, 1)
33  base = np.hypot(x, y)
34  ims = []
35  for add in np.arange(15):
36  ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
37 
38  im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
39  blit=True)
40  #im_ani.save('im.mp4', metadata={'artist':'Guido'})
41 
42 elif example == 3:
43  class Scope:
44  def __init__(self, ax, maxt=2, dt=0.02):
45  self.ax = ax
46  self.dt = dt
47  self.maxt = maxt
48  self.tdata = [0]
49  self.ydata = [0]
50  self.line = ln.Line2D(self.tdata, self.ydata)
51  self.ax.add_line(self.line)
52  self.ax.set_ylim(-.1, 1.1)
53  self.ax.set_xlim(0, self.maxt)
54 
55  def update(self, y):
56  lastt = self.tdata[-1]
57  if lastt > self.tdata[0] + self.maxt: # reset the arrays
58  self.tdata = [self.tdata[-1]]
59  self.ydata = [self.ydata[-1]]
60  self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
61  self.ax.figure.canvas.draw()
62 
63  t = self.tdata[-1] + self.dt
64  self.tdata.append(t)
65  self.ydata.append(y)
66  self.line.set_data(self.tdata, self.ydata)
67  return self.line,
68 
69  def emitter(p=0.03):
70  'return a random value with probability p, else 0'
71  while True:
72  v = np.random.rand(1)
73  if v > p:
74  yield 0.
75  else:
76  yield np.random.rand(1)
77 
78  fig, ax = plt.subplots()
79  scope = Scope(ax)
80 
81  # pass a generator in "emitter" to produce data for the update func
82  ani = animation.FuncAnimation(fig, scope.update, emitter, interval=10,
83  blit=True)
84 
85 elif example == 4:
86  class Scope:
87  def __init__(self, ax, maxt=2, dt=0.02):
88  self.ax = ax
89  self.dt = dt
90  self.maxt = maxt
91  self.tdata = [0]
92  self.ydata = [0]
93  self.line = ln.Line2D(self.tdata, self.ydata)
94  self.ax.add_line(self.line)
95  self.ax.set_ylim(-.1, 1.1)
96  self.ax.set_xlim(0, self.maxt)
97 
98  def update(self, y):
99  lastt = self.tdata[-1]
100  if lastt > self.tdata[0] + self.maxt: # reset the arrays
101  self.tdata = self.tdata[1:]
102  self.ydata = self.ydata[1:]
103  self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
104  self.ax.figure.canvas.draw()
105 
106  t = self.tdata[-1] + self.dt
107  self.tdata.append(t)
108  self.ydata.append(y)
109  self.line.set_data(self.tdata, self.ydata)
110  return self.line,
111 
112  def emitter(p=0.03):
113  'return a random value with probability p, else 0'
114  while True:
115  v = np.random.rand(1)
116  if v > p:
117  yield 0.
118  else:
119  yield np.random.rand(1)
120 
121  fig, ax = plt.subplots()
122  scope = Scope(ax)
123 
124  # pass a generator in "emitter" to produce data for the update func
125  ani = animation.FuncAnimation(fig, scope.update, emitter, interval=10,
126  blit=True)
127 
128 
129 #-----------------------------
130 plt.show()