Skip to main content

Altair -

Altair is a declarative statistical visualization library for Python, built on the powerful Vega and Vega-Lite grammar. It allows you to create interactive, informative charts using a consistent API, where you describe the links between data columns and visual encoding channels (like x-axis, y-axis, color, size) rather than explicitly coding drawing commands.

This guide focuses on the for data visualization. 1. Installation & Setup

One of Altair's strongest features is the ability to create interactivity (like panning, zooming, and tooltips) by linking chart components. altair

import altair as alt import pandas as pd # 1. Data data = pd.DataFrame({'a': ['A', 'B', 'C'], 'b': [28, 55, 43]}) # 2. & 3. Chart + Mark + Encoding chart = alt.Chart(data).mark_bar().encode( x='a', y='b' ) # Display (if in notebook) # chart.show() Use code with caution. Copied to clipboard 3. Data Transformation & Aggregation

Altair works best with tidy data—long-form data where each row is an observation and each column is a variable. Data data = pd

# Create and activate a virtual environment python -m venv altair-venv source altair-venv/bin/activate # On Windows: altair-venv\Scripts\activate # Install Altair and dependencies python -m pip install altair pandas notebook Use code with caution. Copied to clipboard 2. Core Concepts: The Chart Object Every Altair chart follows three basic steps: Pass a pandas DataFrame to alt.Chart() .

# Create a bar chart with the average of column 'b' alt.Chart(data).mark_bar().encode( x='a', y='mean(b)' # Aggregation ) Use code with caution. Copied to clipboard 4. Customizing Your Visualization Data data = pd.DataFrame({'a': ['A'

# Simple interactive tooltip alt.Chart(data).mark_bar().encode( x='a', y='b', tooltip=['a', 'b'] # Add tooltips on hover ).interactive() # Allow zooming/panning Use code with caution. Copied to clipboard 6. Saving Charts