plot.py 1003 B

123456789101112131415161718192021222324252627282930313233343536
  1. from nicegui.elements.element import Element
  2. import justpy as jp
  3. import matplotlib.pyplot as plt
  4. from .element import Element
  5. class Plot(Element):
  6. def __init__(self, close: bool = True, **kwargs):
  7. """Plot
  8. Create a context to configure a `Matplotlib <https://matplotlib.org/>`_ plot.
  9. :param close: weather the figure should be closed after exiting the context; set to False if you want to update it later, default is True
  10. :param kwargs: arguments like `figsize` which should be passed to `pyplot.figure <https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html>`_
  11. """
  12. self.close = close
  13. self.fig = plt.figure(**kwargs)
  14. view = jp.Matplotlib()
  15. view.set_figure(self.fig)
  16. super().__init__(view, '')
  17. def __enter__(self):
  18. plt.figure(self.fig)
  19. return self
  20. def __exit__(self, *_):
  21. self.view.set_figure(plt.gcf())
  22. if self.close:
  23. plt.close(self.fig)