Vega-Altair: A declarative statistical visualization library for Python
Altair is a declarative statistical visualization library for Python, built on top of the powerful Vega-Lite JSON specification. It offers a simple, friendly, and consistent API for creating a wide range of interactive and aesthetically pleasing statistical charts with a minimal amount of code. The library is actively developed, with a current stable version of 6.0.0 and weekly pre-release builds.
Warnings
- breaking Altair 6.0.0 officially removes support for Python 3.8. Users should upgrade to Python 3.9 or newer.
- breaking Altair 6.0.0 is compiled against Vega-Lite version 6. While largely backward compatible, some advanced or undocumented Vega-Lite features might behave differently or require updates if you were directly manipulating the underlying JSON spec.
- breaking The recommended way to access built-in example datasets changed from `from vega_datasets import data` to `from altair.datasets import data` in Altair 6.0.0. Some column names in these datasets might also have changed (e.g., spaces instead of underscores).
- gotcha Altair charts are primarily designed for display in Jupyter environments (Notebook, Lab, VS Code with Jupyter extension). Outside of these, you may need to configure a specific renderer (e.g., 'mimetype', 'html', 'json', 'altair_saver') to display or save charts.
Install
-
pip install altair -
pip install "altair[all]"
Imports
- Chart
import altair as alt
- data
from altair.datasets import data
Quickstart
import altair as alt
import pandas as pd
# Sample data
data = pd.DataFrame({
'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
# Create a simple bar chart
chart = alt.Chart(data).mark_bar().encode(
x='a',
y='b'
)
chart.show()