line_plot.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from typing import List
  2. from .pyplot import Pyplot
  3. class LinePlot(Pyplot):
  4. def __init__(self, *, n: int = 1, limit: int = 100, update_every: int = 1, close: bool = True, **kwargs) -> None:
  5. """Line Plot
  6. Create a line plot using pyplot.
  7. The `push` method provides live updating when utilized in combination with `ui.timer`.
  8. :param n: number of lines
  9. :param limit: maximum number of datapoints per line (new points will displace the oldest)
  10. :param update_every: update plot only after pushing new data multiple times to save CPU and bandwidth
  11. :param close: whether the figure should be closed after exiting the context; set to `False` if you want to update it later (default: `True`)
  12. :param kwargs: arguments like `figsize` which should be passed to `pyplot.figure <https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html>`_
  13. """
  14. super().__init__(close=close, **kwargs)
  15. self.x: List[float] = []
  16. self.Y: List[List[float]] = [[] for _ in range(n)]
  17. self.lines = [self.fig.gca().plot([], [])[0] for _ in range(n)]
  18. self.slice = slice(0 if limit is None else -limit, None)
  19. self.update_every = update_every
  20. self.push_counter = 0
  21. def with_legend(self, titles: List[str], **kwargs):
  22. self.fig.gca().legend(titles, **kwargs)
  23. self._convert_to_html()
  24. return self
  25. def push(self, x: List[float], Y: List[List[float]]) -> None:
  26. self.push_counter += 1
  27. self.x = [*self.x, *x][self.slice]
  28. for i in range(len(self.lines)):
  29. self.Y[i] = [*self.Y[i], *Y[i]][self.slice]
  30. if self.push_counter % self.update_every != 0:
  31. return
  32. for i in range(len(self.lines)):
  33. self.lines[i].set_xdata(self.x)
  34. self.lines[i].set_ydata(self.Y[i])
  35. flat_y = [y_i for y in self.Y for y_i in y]
  36. min_x = min(self.x)
  37. max_x = max(self.x)
  38. min_y = min(flat_y)
  39. max_y = max(flat_y)
  40. pad_x = 0.01 * (max_x - min_x)
  41. pad_y = 0.01 * (max_y - min_y)
  42. self.fig.gca().set_xlim(min_x - pad_x, max_x + pad_x)
  43. self.fig.gca().set_ylim(min_y - pad_y, max_y + pad_y)
  44. self._convert_to_html()
  45. self.update()