Plotting
check_dependencies()
marimo supports several popular plotting libraries, including matplotlib, plotly, seaborn, and altair.
This tutorial gives examples using matplotlib; other libraries are used similarly. ## Matplotlib To show a plot, include it in the last expression of a cell (just like any other output).
# create the plot in the last line of the cell
import matplotlib.pyplot as plt
plt.plot([1, 2])
plt.plot(%5B1%2C%202%5D)
# create a plot
plt.plot([1, 2])
# ... do some work ...
# make plt.gca() the last line of the cell
plt.gca()
plt.plot(%5B1%2C%202%5D)%0A%23%20...%20do%20some%20work%20...%0A%23%20make%20plt.gca()%20the%20last%20line%20of%20the%20cell%0Aplt.gca()
You can use plt.show()
or figure.show()
to display
plots in the console area of a cell. Keep in mind that console
outputs are not shown in the app view.
mo.accordion(plt_show_explainer)
A new figure every cell. Every cell starts with an empty figure for the imperative pyplot
API.
x%20%3D%20np.linspace(start%3D-4%2C%20stop%3D4%2C%20num%3D100%2C%20dtype%3Dfloat)
plt.plot(x%2C%20x)%0Aplt.plot(x%2C%20x**2)%0Aplt.gca()
plt.plot(x%2C%20x**3)
To build a figure over multiple cells, use the object-oriented API and create your own axis:
_%2C%20axis%20%3D%20plt.subplots()%0Aaxis.plot(x%2C%20x)%0Aaxis.plot(x%2C%20x**2)%0Aaxis
axis.plot(x%2C%20x**3)%0Aaxis
Draw plots interactively
Draw plots interactively by parametrizing them with UI elements.
Visualizing powers.
exponent%20%3D%20mo.ui.slider(1%2C%205%2C%20value%3D1%2C%20step%3D1%2C%20label%3D'exponent')%0A%0Amo.md(%0A%20%20%20%20f%22%22%22%0A%20%20%20%20**Visualizing%20powers.**%0A%0A%20%20%20%20%7Bexponent%7D%0A%20%20%20%20%22%22%22%0A)
||[f(x) = x||]
import%20functools%0A%0A%0A%40functools.cache%0Adef%20_plot(exponent)%3A%0A%20%20%20%20plt.plot(x%2C%20x**exponent)%0A%20%20%20%20return%20plt.gca()%0A%0A%0A_tex%20%3D%20(%0A%20%20%20%20f%22%24%24f(x)%20%3D%20x%5E%7Bexponent.value%7D%24%24%22%20if%20exponent.value%20%3E%201%20else%20%22%24%24f(x)%20%3D%20x%24%24%22%0A)%0A%0Amo.md(%0A%20%20%20%20f%22%22%22%0A%0A%20%20%20%20%7B_tex%7D%0A%0A%20%20%20%20%7Bmo.as_html(_plot(exponent.value))%7D%0A%20%20%20%20%22%22%22%0A)
Other libraries
marimo also supports these other plotting libraries:
Just output their figure objects as the last expression of a cell, or embed them in markdown with mo.as_html
.
If you would like another library to be integrated into marimo, please get in touch.
module_not_found_explainer%20%3D%20mo.md(%0A%20%20%20%20%22%22%22%0A%20%20%20%20%23%23%20Oops!%0A%0A%20%20%20%20It%20looks%20like%20you're%20missing%20a%20package%20that%20this%20tutorial%20%0A%20%20%20%20requires.%0A%0A%20%20%20%20Close%20marimo%2C%20install%20**%60numpy%60**%20and%20**%60matplotlib%60**%2C%20then%20%0A%20%20%20%20open%20this%20tutorial%20once%20more.%0A%0A%20%20%20%20If%20you%20use%20%60pip%60%2C%20run%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20pip%20install%20numpy%20matplotlib%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20at%20your%20command%20line.%0A%20%20%20%20%22%22%22%0A).callout(kind%3D'warn')%0A%0Adef%20check_dependencies()%3A%0A%20%20%20%20if%20missing_packages%3A%0A%20%20%20%20%20%20%20%20return%20module_not_found_explainer
plt_show_explainer%20%3D%20%7B%0A%20%20%20%20%22Using%20%60plt.show()%60%22%3A%20%22%22%22%0A%20%20%20%20You%20can%20use%20%60plt.show()%60%20or%20%60figure.show()%60%20to%20display%0A%20%20%20%20plots%20in%20the%20console%20area%20of%20a%20cell.%20Keep%20in%20mind%20that%20console%0A%20%20%20%20outputs%20are%20not%20shown%20in%20the%20app%20view.%0A%20%20%20%20%22%22%22%0A%7D
try%3A%0A%20%20%20%20import%20matplotlib%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20missing_packages%20%3D%20False%0Aexcept%20ModuleNotFoundError%3A%0A%20%20%20%20missing_packages%20%3D%20True%0A%0Aif%20not%20missing_packages%3A%0A%20%20%20%20matplotlib.rcParams%5B'figure.figsize'%5D%20%3D%20(6%2C%202.4)
import%20marimo%20as%20mo
Back to top