What it is
Altair is a declarative statistical visualization library for Python. It allows users to create interactive and concise charts based on the Vega and Vega-Lite visualization grammars.
Altair leverages a declarative API where you specify 'what' to plot rather than 'how' to plot it. It supports bar charts, line charts, scatter plots, heatmaps, and more, with built-in interactivity like selections, tooltips, and filtering.
Installation
pip install altairGetting started
The smallest useful thing you can do with it, and what each part means.
import altair as alt
import pandas as pd
df = pd.DataFrame({'category': ['A', 'B', 'C'], 'value': [4, 7, 1]})
chart = alt.Chart(df).mark_bar().encode(x='category', y='value')
chart.show()import altair as alt
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [10, 15, 13, 17]})
chart = alt.Chart(df).mark_line(point=True).encode(x='x', y='y', tooltip=['x','y'])
chart.show()Advanced usage
Where the library earns its place over a simpler alternative.
import altair as alt
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4],'y':[10,20,25,30],'category':['A','A','B','B']})
selector = alt.selection_multi(fields=['category'])
chart = alt.Chart(df).mark_circle(size=100).encode(x='x', y='y', color='category').add_selection(selector)
chart.show()import altair as alt
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4],'y':[10,20,25,30],'group':['A','A','B','B']})
chart = alt.Chart(df).mark_line().encode(x='x', y='y').facet('group')
chart.show()import altair as alt
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4],'y':[10,20,25,30],'group':['A','A','B','B']})
input_dropdown = alt.binding_select(options=['A','B'], name='Select Group:')
selection = alt.selection_single(fields=['group'], bind=input_dropdown)
chart = alt.Chart(df).mark_bar().encode(x='x', y='y', color='group').add_selection(selection).transform_filter(selection)
chart.show()Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- ValueError: Data format not recognized
- Ensure the input data is a Pandas DataFrame or a compatible data format.
- AltairError: chart has no encodings
- Make sure to specify at least one encoding (x, y, color, etc.) for the chart.
- Renderer not found
- Use `chart.show()` in Jupyter or `alt.renderers.enable('default')` to specify a renderer.
Best practices
- Use Pandas DataFrames as input data for better integration.
- Leverage declarative syntax to keep code clean and readable.
- Combine charts with layering and faceting for richer visualizations.
- Use selections and interactions to enhance user exploration.
- Export charts to HTML or JSON for embedding in web applications.
Background
Why it exists, and what it was reacting to.
Altair was created by Jake VanderPlas and the Altair development team to provide a simple, declarative way to create rich visualizations in Python. Its focus is on producing high-quality, interactive charts with minimal code while maintaining clear semantics and good integration with Pandas.
