How to update matplotlib #5532
-
First Check
Example Codeimport numpy as np
from matplotlib import pyplot as plt
from nicegui import ui
with ui.pyplot(close=False) as plot:
x = np.linspace(0.0, 5.0)
y = np.cos(2 * np.pi * x) * np.exp(-x)
plt.plot(x, y, "-")
ax = plt.gca()
def add_line():
ax.axhline(0, color="red", linestyle="--")
plot.update()
ui.button("Add line at y=0", on_click=add_line)
ui.run()DescriptionI expected this to plot the line on click. NiceGUI Version3.2.0 Python Version3.13.9 BrowserChrome Operating SystemWindows Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Dec 2, 2025
Replies: 1 comment 1 reply
-
|
Ok, found it: #2892, but I still do not understand why my solution did not work. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the main reason is that it is not implemented this way. In contrast to using a with-expression,
update()doesn't translate the figure into HTML, so there is no new content to be sent to the client. The intention is to useui.pyplotwith Matplotlibs "pyplot" API (see https://matplotlib.org/stable/users/explain/figure/api_interfaces.html).For the object-oriented API, I'd recommend using
ui.matplotlib: