Altair

Language: Python

Data Science

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.

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.

Installation

pip: pip install altair
conda: conda install -c conda-forge altair

Usage

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.

Simple Bar Chart

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()

Creates a simple bar chart using a Pandas DataFrame and displays it.

Line Chart with Tooltips

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()

Generates a line chart with points and interactive tooltips showing x and y values.

Scatter Plot with Selection

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()

Creates an interactive scatter plot where users can select points by category using a selection object.

Faceted Charts

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()

Facets data into multiple small charts based on the 'group' column.

Interactive Filtering

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()

Demonstrates interactive filtering using a dropdown selection to display only selected group data.

Error Handling

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.