Introduction to Altair
Altair (visualization library) is a Python library used for creating interactive statistical
visualizations. It is based on the grammar of graphics and allows users to create charts using
a declarative approach.
Altair is built on top of Vega-Lite, which means the user describes what the visualization
should look like, and Altair determines how to render it.
Instead of writing many commands to draw plots step-by-step, you simply declare the
relationship between data fields and visual properties.
The three main elements are:
Data
Mark
Encoding
This approach makes Altair a powerful and easy-to-use visualization tool for Python users.
Declarative API: A Declarative API allows the user to describe the desired output, rather than
specifying the detailed steps to achieve it.
Procedural Approach Declarative Approach
Specifies how to create the plot step-by-step Specifies what the plot should represent
Requires multiple plotting commands Uses a simple specification
Example: matplotlib Example: Altair
Procedural style:
Create figure
Add axes
Plot data
Declarative style:
Map data field → visual property
Example:
x-axis = year
y-axis = sales
mark = line
Basic Components of Altair Declarative API
Altair visualizations are created using three main components:
, 1. Data
The dataset used for visualization.
Example sources:
Pandas DataFrame
CSV file
JSON data
Example concept:
data = dataframe
2. Mark
A mark defines the type of graphical element used to represent data.
Common mark types:
Mark Type Description
mark_point() Scatter plot points
mark_line() Line chart
mark_bar() Bar chart
mark_area() Area chart
mark_circle() Circular points
mark_tick() Tick marks
Example idea:
mark_bar()
3. Encoding
Encoding maps data fields to visual properties such as:
Encoding Meaning
x Horizontal axis
y Vertical axis
color Color of marks