Matplotlib
Display a matplotlib figure.
@solara.component
def FigureMatplotlib(
figure,
dependencies: List[Any] = None,
format: str = "svg",
**kwargs,
):
...
Example
import solara
from matplotlib.figure import Figure
@solara.component
def Page():
fig = Figure()
ax = fig.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
return solara.FigureMatplotlib(fig)
Live output
When running under solara-server, we by default configure the same 'inline' backend as in the Jupyter notebook.
For performance reasons, you might want to pass in a list of dependencies that indicate when the figure changed, to avoid re-rendering it on every render.
Example using pyplot
Note that it is also possible to use the pyplot interface, but be sure to close the figure not to leak memory.
import solara
import matplotlib.pyplot as plt
@solara.component
def Page():
plt.figure()
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
plt.close()
Live output
Note that the figure is not the same size using the pyplot interface, due to the default figure size being different.
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
2
0.1