Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import numpy as np
  2. from collections import deque
  3.  
  4. import matplotlib.pyplot as plt
  5. import matplotlib.animation as animation
  6. from matplotlib.widgets import Button, Slider
  7.  
  8.  
  9. class AnalogPlot:
  10. def __init__(self, data, display_len):
  11. self.buff = deque(np.zeros(display_len))
  12. self.display_len = display_len
  13. self.data = data
  14.  
  15. # set up the plot
  16. self.fig = plt.figure()
  17. self.ax = self.fig.add_subplot(111, xlim=(0, t_max), ylim=(-1, 1))
  18. self.ax.set_xticks((0,t_max))
  19. self.lines = self.ax.plot([], [])
  20.  
  21. # setup the animation
  22. self.cur_frame = 0
  23. self.anim = animation.FuncAnimation(self.fig, self._update,
  24. interval=1.0)
  25.  
  26. # setup the animation control
  27. self.anim_running = True
  28.  
  29.  
  30. def _add_to_buff(self, buf, val):
  31. if len(buf) < self.display_len:
  32. buf.appendLeft(val)
  33. else:
  34. buf.popleft()
  35. buf.append(val)
  36.  
  37. def _update(self, frame):
  38. frame = self.cur_frame
  39. self._add_to_buff(self.buff, self.data[frame:frame+1])
  40. self.lines[0].set_data(range(self.display_len), self.buff)
  41.  
  42. self.ax.set_xticklabels((str(frame), str(frame+self.display_len)))
  43.  
  44. self.time_slider.eventson = False
  45. self.time_slider.set_val(frame)
  46. self.time_slider.eventson = True
  47.  
  48. self.cur_frame += 1
  49.  
  50. return self.lines
  51.  
  52. def _pause(self, event):
  53. if self.anim_running:
  54. self.anim.event_source.stop()
  55. self.anim_running = False
  56. else:
  57. self.anim.event_source.start()
  58. self.anim_running = True
  59.  
  60. def _reset(self, event):
  61. self._set_val(0)
  62.  
  63.  
  64. def _set_val(self, frame=0):
  65. frame = int(frame)
  66. self.cur_frame = frame
  67. new_start = frame - self.display_len
  68. if new_start >= 0:
  69. self.buff = deque(self.data[new_start:frame])
  70. else:
  71. self.buff = deque(np.concatenate((np.zeros(np.abs(new_start)), self.data[:frame])))
  72.  
  73. self.anim.event_source.stop()
  74. self.anim = animation.FuncAnimation(self.fig, self._update,
  75. interval=1.0)
  76. self.anim_running = True
  77.  
  78.  
  79. def animate(self):
  80. pause_ax = self.fig.add_axes((0.7, 0.025, 0.1, 0.04))
  81. pause_button = Button(pause_ax, 'pause', hovercolor='0.975')
  82. pause_button.on_clicked(self._pause)
  83.  
  84. reset_ax = self.fig.add_axes((0.8, 0.025, 0.1, 0.04))
  85. reset_button = Button(reset_ax, 'reset', hovercolor='0.975')
  86. reset_button.on_clicked(self._reset)
  87.  
  88. slider_ax = self.fig.add_axes((0.1, 0.025, 0.5, 0.04))
  89. self.time_slider = Slider(slider_ax, label='Time',
  90. valmin=0, valmax=self.data.shape[0],
  91. valinit=0.0)
  92.  
  93. self.time_slider.on_changed(self._set_val)
  94.  
  95. plt.show()
  96.  
  97.  
  98. t_max = 100
  99. lin_sig = np.linspace(0, 1, 1000)
  100. analog_plot = AnalogPlot(lin_sig, t_max)
  101. analog_plot.animate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement