Altair Saving & Exporting

๐Ÿ’พ Basic save

chart.save("chart.html")      # interactive, self-contained โ€” always works, no extra deps
chart.save("chart.json")         # raw Vega-Lite spec
chart.save("chart.png")             # static image โ€” needs extra setup, see below
chart.save("chart.svg")                # static vector โ€” needs extra setup, see below
chart.save("chart.pdf")                   # needs extra setup, see below

PNG/SVG/PDF export requires an extra dependency ( vl-convert-python is the modern, recommended one). HTML and JSON work with zero extra setup.

pip install vl-convert-python

๐ŸŽฏ Resolution for PNG export

chart.save("chart.png", ppi=300)      # higher resolution for print quality

๐Ÿ–ผ๏ธ Embedding in HTML manually

chart.save("chart.html", embed_options={"actions": False})   # hides the "..." export menu in the corner

๐Ÿ““ In Jupyter

chart                # just the last expression in a cell โ€” auto-renders interactively

No .show() or plt.show() equivalent needed inside Jupyter itself.

๐ŸŒ Outside Jupyter (plain script)

chart.show()      # opens in default browser via a local server

๐Ÿ“„ Getting the raw spec (for embedding elsewhere, e.g. a web app)

spec = chart.to_dict()          # Python dict of the full Vega-Lite spec
spec_json = chart.to_json()        # as a JSON string

to_dict()/to_json() is the bridge to using an Altair-built chart inside a JS frontend (Vega-Lite runtime), a Streamlit app, or any tool that accepts a raw Vega-Lite spec.

๐Ÿ–ฅ๏ธ Renderers (how charts get displayed)

alt.renderers.enable("default")     # standard, works in Jupyter/JupyterLab
alt.renderers.enable("mimetype")       # alternative for some notebook environments
print(alt.renderers.names())              # list available renderers

๐Ÿงฎ Row limit warning

Altair embeds data directly in the chart spec and defaults to a 5,000-row limit, throwing a MaxRowsError beyond that. For larger data: aggregate before plotting, or disable the check.

alt.data_transformers.disable_max_rows()      # removes the limit โ€” fine for moderate overages, risky for huge data
alt.data_transformers.enable("vegafusion")     # better long-term fix: pushes aggregation to a backend, handles large data properly

Altair Styling & Themes ยท Altair