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.
Common errors
-
ModuleNotFoundError: No module named 'altair'
cause The Altair library is not installed in the Python environment.fixInstall Altair using pip: `pip install altair`. -
AttributeError: module 'altair' has no attribute 'selection_point'
cause The 'selection_point' attribute is not available in the installed version of Altair.fixUpgrade Altair to the latest version: `pip install --upgrade altair`. -
AttributeError: module 'altair' has no attribute 'Chart'
cause A local file named 'altair.py' is shadowing the Altair library.fixRename or remove the local 'altair.py' file to avoid the naming conflict. -
AttributeError: module 'jsonschema' has no attribute 'Draft7Validator'
cause An outdated version of the 'jsonschema' library is installed.fixUpgrade 'jsonschema' to a compatible version: `pip install 'jsonschema>=3.0,<4.0'`. -
ModuleNotFoundError: No module named 'altair.vegalite.v4'
cause The installed version of Altair does not include the 'vegalite.v4' module.fixEnsure compatibility between Altair and dependent libraries by installing compatible versions or upgrading dependent libraries.
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.
- breaking Building `vegafusion` (a dependency of Altair) on Alpine Linux environments may fail due to missing `glibc` or `libgcc` components required by Rust/Cargo build tools. This typically manifests as 'Error loading shared library libgcc_s.so.1: No such file or directory' errors during metadata generation.
Install
-
pip install altair -
pip install "altair[all]"
Imports
- Chart
import altair as alt
- data
from vega_datasets import 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()