Matplotlib
Display a matplotlib figure.
@solara.component
def FigureMatplotlib(
figure,
dependencies: List[Any] = None,
format: str = "svg",
**kwargs,
):
...
We recomment not to use the pyplot interface, but rather to create a figure directly, e.g:
import reacton
import solara as sol
from matplotlib.figure import Figure
@solara.component
def Page():
# do this instead of plt.figure()
fig = Figure()
ax = fig.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
return solara.FigureMatplotlib(fig)
You should also avoid drawing using the pyplot interface, as it is not thread-safe. If you do use it, your drawing might be corrupted due to another thread/user drawing at the same time.
If you still must use pyplot to create the figure, make sure you call plt.switch_backend("agg")
before creating the figure, to avoid starting an interactive backend.
For performance reasons, you might want to pass in a list of depenendencies that indicate when the figure changed, to avoid re-rendering it on every render.
Arguments
figure
: Matplotlib figure.dependencies
: List of dependencies to watch for changes, if None, will convert the figure to a static image on each render.format
: The image format to to convert the Matplotlib figure to (png, jpg, svg, etc.)kwargs
: Additional arguments to passed to figure.savefig
Example