displayHandler.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib import animation
  4. class Plot:
  5. def __init__(self, count):
  6. self.fig, self.plots = plt.subplots(nrows=count, ncols=1)
  7. self.fig.tight_layout()
  8. self.fig.canvas.set_window_title('FFT Cube')
  9. self.count = count
  10. self.entities = []
  11. def addLabel(self, ax, title="", xlabel="", ylabel=""):
  12. self.plots[ax].set_title(title)
  13. self.plots[ax].set_xlabel(xlabel)
  14. self.plots[ax].set_ylabel(ylabel)
  15. def addLine(self, ax, x, yMin, yMax, color = 'r-', xscale = 'linear'):
  16. self.plots[ax].set_xlim(min(x), max(x))
  17. self.plots[ax].set_ylim(yMin, yMax)
  18. self.plots[ax].set_xscale(xscale)
  19. line, = self.plots[ax].plot(x, [0]*len(x), color)
  20. self.entities.append(line)
  21. return line
  22. def addBars(self, ax, labels, yMin, yMax, width, offset, color = 'r'):
  23. self.plots[ax].set_ylim(yMin, yMax)
  24. self.plots[ax].set_xticklabels(labels)
  25. x = range(len(labels))
  26. y = [0] * (len(labels))
  27. bars = self.plots[ax].bar([i+offset for i in x], y, width, color=color)
  28. for bar in bars:
  29. self.entities.append(bar)
  30. return bars
  31. def update(self, frame = None):
  32. self._cb()
  33. return self.entities
  34. def show(self, cb, framerate = 60):
  35. self._cb = cb
  36. self.animation = animation.FuncAnimation(self.fig, self.update, interval=1000/framerate, blit=True)
  37. plt.show()