import polars as pl
from pyobsplot import Plot
= pl.read_csv("data/penguins.csv")
penguins
Plot.plot(
{"grid": True,
"marks": [
Plot.dot(
penguins,
{"x": "flipper_length_mm",
"y": "body_mass_g",
"fill": "sex",
"tip": True,
},
)
],
} )
pyobsplot
Overview
pyobsplot
allows to use Observable Plot to create charts in Jupyter notebooks, VSCode notebooks, Google Colab and Quarto documents. Plots are created from Python code with a syntax as close as possible to the JavaScript one.
Or, for a bit more complex example:
Plot.plot(
{"marginLeft": 75,
"marginRight": 70,
"x": {"insetRight": 10},
"y": {"grid": True},
"facet": {"marginRight": 70},
"marks": [
0]),
Plot.ruleX([
Plot.barX(
penguins,
Plot.groupY("x": "count"}, {"fy": "island", "y": "species", "fill": "sex"}
{
),
),
Plot.text("The Adelie species is the only one on Torgersen Island."],
[
{"fy": ["Torgersen"],
"frameAnchor": "right",
"lineWidth": 16,
"dx": -4,
},
),
],
} )
Installation and usage
Getting started gives installation instructions and a quick usage overview.
Usage gives more detailed usage instructions.
If you just want to try this package without installing it on your computer, you can open an introduction notebook in Google Colab:
Features and limitations
Features:
- Syntax as close as possible to the JavaScript one
- Two renderers available:
widget
, which generates plots as Jupyter widgets, andjsdom
, which generates SVG or HTML outputs - Pandas and polars DataFrame and Series objects are serialized using Arrow IPC format for improved speed and better data type conversions
- Works offline, no iframe or dependency to Observable runtime
- Caching mechanism of data objects if they are used several times in the same plot
- Custom JavaScript code can be passed as strings with the
js
method - Python
date
anddatetime
objects are automatically converted to JavaScriptDate
objects - Plots can be defined with a dictionary, a call to a
Plot
mark function, or withkwargs
. See alternative syntaxes. - Works with Jupyter notebooks and Quarto HTML documents. Plots without legends are also supported in PDF and docx outputs with the
jsdom
renderer. - Plots can be saved to SVG or HTML files
Limitations:
Credits
- Observable Plot, developed by Mike Bostock and Philippe Rivière among others.
- The widget is developed thanks to the anywidget framework.
- Some code from the
jsdom
renderer has been adapted from altair_saver. - This documentation website is generated by Quarto.