Bladeren bron

moved plot into main.py

Rodja Trappe 4 jaren geleden
bovenliggende
commit
68f5dfc14c
3 gewijzigde bestanden met toevoegingen van 17 en 23 verwijderingen
  1. 0 16
      README.md
  2. 11 0
      main.py
  3. 6 7
      nicegui/elements/plot.py

+ 0 - 16
README.md

@@ -50,22 +50,6 @@ API Reference is hosted at [https://nicegui.io](https://nicegui.io). Also have a
 
 
 ### Plots
 ### Plots
 
 
-<img src="https://raw.githubusercontent.com/zauberzeug/nicegui/main/sceenshots/demo-plot.png" width="300" align="right">
-To render a simple plot you create a new context and call the neccessary [Matplotlib](https://matplotlib.org/) functions:
-
-```python
-from nicegui import ui
-from matplotlib import pyplot as plt
-import numpy as np
-
-with ui.plot():
-    x = np.linspace(0.0, 5.0)
-    y = np.cos(2 * np.pi * x) * np.exp(-x)
-    plt.plot(x, y, '-')
-    plt.xlabel('time (s)')
-    plt.ylabel('Damped oscillation')
-```
-
 To update a plot in regular intervals, have look at [main.py](https://github.com/zauberzeug/nicegui/tree/main/main.py).
 To update a plot in regular intervals, have look at [main.py](https://github.com/zauberzeug/nicegui/tree/main/main.py).
 
 
 <img src="https://raw.githubusercontent.com/zauberzeug/nicegui/main/sceenshots/demo-live-plot.gif" width="300" align="right">
 <img src="https://raw.githubusercontent.com/zauberzeug/nicegui/main/sceenshots/demo-live-plot.gif" width="300" align="right">

+ 11 - 0
main.py

@@ -90,3 +90,14 @@ with example(ui.input):
         on_change=lambda e: result.set_text('you typed: ' + e.value)
         on_change=lambda e: result.set_text('you typed: ' + e.value)
     )
     )
     result = ui.label('')
     result = ui.label('')
+
+with example(ui.plot):
+    from matplotlib import pyplot as plt
+    import numpy as np
+
+    with ui.plot(figsize=(2.5, 1.6)):
+        x = np.linspace(0.0, 5.0)
+        y = np.cos(2 * np.pi * x) * np.exp(-x)
+        plt.plot(x, y, '-')
+        plt.xlabel('time (s)')
+        plt.ylabel('Damped oscillation')

+ 6 - 7
nicegui/elements/plot.py

@@ -5,17 +5,16 @@ from .element import Element
 
 
 class Plot(Element):
 class Plot(Element):
 
 
-    def __init__(self, close: bool = True):
-        """Create a context to configure a Matplotlib Plot.
+    def __init__(self, close: bool = True, **kwargs):
+        """Plot
 
 
-        Parameters
-        ----------
-        close : boolean, optional
-            weather the figure should be closed after exiting the context; set to False if you want to update it later
+        Create a context to configure a `Matplotlib <https://matplotlib.org/>`_ plot
+
+        :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
         """
         """
 
 
         self.close = close
         self.close = close
-        self.fig = plt.figure()
+        self.fig = plt.figure(**kwargs)
 
 
         view = jp.Matplotlib()
         view = jp.Matplotlib()
         view.set_figure(self.fig)
         view.set_figure(self.fig)