plotly_documentation.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import numpy as np
  2. from nicegui import ui
  3. from ..documentation_tools import text_demo
  4. def main_demo() -> None:
  5. import plotly.graph_objects as go
  6. fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))
  7. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  8. ui.plotly(fig).classes('w-full h-40')
  9. def more() -> None:
  10. @text_demo('Dictionary interface', '''
  11. This demo shows how to use the declarative dictionary interface to create a plot.
  12. For plots with many traces and data points, this is more efficient than the object-oriented interface.
  13. The definition corresponds to the JavaScript Plotly API, see [https://plotly.com/javascript/](https://plotly.com/javascript/).
  14. Due to different defaults, the resulting plot may look slightly different from the same plot created with the object-oriented interface,
  15. but the functionality is the same.
  16. ''')
  17. def plot_dict_interface():
  18. fig = {
  19. "data": [
  20. {
  21. "type": "scatter",
  22. "name": "Trace 1",
  23. "x": [1, 2, 3, 4],
  24. "y": [1, 2, 3, 2.5]
  25. },
  26. {
  27. "type": "scatter",
  28. "name": "Trace 2",
  29. "x": [1, 2, 3, 4],
  30. "y": np.array([1.4, 1.8, 3.8, 3.2]),
  31. "line": {
  32. "dash": 'dot',
  33. "width": 3
  34. }
  35. }
  36. ],
  37. "layout": {
  38. "margin": {"l": 20, "r": 0, "t": 0, "b": 15},
  39. "plot_bgcolor": "#E5ECF6",
  40. "xaxis": {
  41. "gridcolor": "white",
  42. },
  43. "yaxis": {
  44. "gridcolor": "white",
  45. }
  46. }
  47. }
  48. ui.plotly(fig).classes('w-full h-40')
  49. @text_demo('Plot updates', '''
  50. This demo shows how to update the plot in real time.
  51. Click the button to add a new trace to the plot.
  52. To send the new plot to the browser, make sure to explicitly call `plot.update()` or `ui.update(plot)`.
  53. ''')
  54. def plot_updates():
  55. import numpy as np
  56. import plotly.graph_objects as go
  57. fig = go.Figure()
  58. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  59. plot = ui.plotly(fig).classes('w-full h-40')
  60. def add_trace():
  61. fig.add_trace(go.Scatter(x=np.arange(10), y=np.random.randn(10)))
  62. plot.update()
  63. ui.button('Add trace', on_click=add_trace)