Introduction to Altair in Python
Last Updated :
19 Jul, 2025
Altair is a declarative statistical visualization library in Python, designed to make it easy to create clear and informative graphics with minimal code. Built on top of Vega-Lite, Altair focuses on simplicity, readability and efficiency, making it a favorite among data scientists and analysts.
Why Use Altair?
Unlike imperative libraries such as matplotlib or seaborn, where you must specify how every element of the plot should be constructed, Altair allows you to focus on what you want to visualize the data and its relationships. This declarative approach:
- Leads to cleaner and more readable code
- Automatically manages axes, legends and scales
- Encourages exploratory data analysis and interactive visualizations
Installation
Altair can be installed like any other Python library using:
pip install altair
We’ll also use datasets from the vega_datasets package. To install it:
pip install vega_datasets
Note: Altair visualizations render using JavaScript. It's best to use Jupyter Notebook, JupyterLab, VSCode Notebooks, or any notebook-supported IDE to visualize charts.
Essential Elements of an Altair Chart
All altair charts need three essential elements: Data, Mark and Encoding. A valid chart can also be made by specifying only the data and mark. The basic format of all altair chart is:
alt.Chart(data).mark_bar().encode(
encoding1 = 'column1',
encoding2 = 'column2',
)
1. Data
Altair is built to work seamlessly with Pandas DataFrames, making data encoding intuitive and straightforward. When you create a chart, the first argument is usually your dataset. While DataFrames are recommended for ease of use, Altair also supports other data formats:
- Altair Data Objects like UrlData, InlineData or NamedData
- CSV or JSON files (as text or from a URL)
- Geospatial Data supporting __geo_interface__ (e.g., GeoPandas, GeoJSON)
Example:
Python
import pandas as pd
df = pd.DataFrame({
'Category': ['A', 'B', 'C'],
'Value': [10, 20, 30]
})
2. Mark
In Altair, the mark defines how your data should be visually represented such as bars, lines, points, or areas. It is one of the essential components of any Altair chart. You specify a mark using the .mark_*() method, where * is the type of visualization you want.
Common Mark Types
Mark Type | Description |
---|
mark_bar() | Bar chart (for categorical data) |
---|
mark_line() | Line chart (for trends over time) |
---|
mark_point() | Scatter plot (for dot data points) |
---|
mark_area() | Area chart (for cumulative values) |
---|
mark_tick() | Tick marks (minimalist visuals) |
---|
mark_text() | Display text on the chart |
---|
Example:
Python
alt.Chart(df).mark_bar(color='steelblue').encode(
x='Category',
y='Value'
)
3. Encoding
Encoding in Altair maps data columns to visual properties using .encode(). Common channels include x and y for axes, color, size, shape and opacity for styling, tooltip for hover info, row/column for faceting, href for links and text for labels. Type hints guide Altair in interpreting data:
- Q: Quantitative (numbers)
- N: Nominal (categories)
- O: Ordinal (ordered)
- T: Temporal (dates/times)
Python
alt.Chart(df).mark_point().encode(
x='x_column',
y='y_column',
color='category_column',
tooltip=['x_column', 'y_column', 'category_column']
)
Examples
Example 1 : Simple Bar Chart
Python
import altair as alt
import pandas as pd
d = pd.DataFrame({
'Website': ['StackOverflow', 'FreeCodeCamp', 'GeeksForGeeks', 'MDN', 'CodeAcademy'],
'Score': [65, 50, 99, 75, 33]
})
chart = alt.Chart(d).mark_bar().encode(
x='Website',
y='Score'
)
chart
Output
Simple Bar Chart using AltairExplanation: Chart is initialized with alt.Chart(d), the bar type is set using .mark_bar() and the axes are encoded with ‘Website’ on the x-axis and ‘Score’ on the y-axis. Displaying the chart renders bars that represent the scores for each website.
Example 2: Interactive Scatter Plot (Iris Dataset)
Python
import altair as alt
from vega_datasets import data
iris = data.iris()
chart = alt.Chart(iris).mark_point().encode(
x='sepalLength',
y='petalLength',
shape='species',
color='species',
tooltip=['sepalLength', 'petalLength', 'species']
)
chart
Output
Scatter plot using AltairExplanation: Chart is initialized with alt.Chart(iris) and .mark_point() to create a scatter plot. encode() maps sepalLength to the x-axis, petalLength to the y-axis and uses shape and color to distinguish species. Tooltips provide interactive details on hover.
Example 3: Line Chart (Temporal Data)
Python
import altair as alt
from vega_datasets import data
stocks = data.stocks()
chart = alt.Chart(stocks).mark_line(point=True).encode(
x='date:T',
y='price:Q',
color='symbol:N',
tooltip=['date:T', 'price:Q', 'symbol:N']
).properties(
title='Stock Prices Over Time'
)
chart
Output
Line chart using AltairExplanation: The stocks dataset is visualized as a line chart using mark_line(point=True). Time (date) is encoded on the x-axis and stock price on the y-axis. Different symbols (stock names) are colored distinctly.
Example 4: Faceted Bar Chart
Python
import altair as alt
from vega_datasets import data
cars = data.cars()
chart = alt.Chart(cars).mark_bar().encode(
x='Cylinders:O',
y='mean(Miles_per_Gallon):Q',
color='Cylinders:O'
).facet(
column='Origin:N'
).properties(
title='Average MPG by Cylinders'
)
chart
Output
Faceted Bar Chart using AltairExplanation: Chart is created with alt.Chart(cars) and .mark_bar() to show bars. Cylinders is mapped to the x-axis, average M, andiles_per_Gallon to the y-axis and bars are colored by cylinder count. Faceting by Origin enables regional comparison.
Related Articles
Introduction to Altair in Python
Similar Reads
Introduction to Altair in Python Altair is a declarative statistical visualization library in Python, designed to make it easy to create clear and informative graphics with minimal code. Built on top of Vega-Lite, Altair focuses on simplicity, readability and efficiency, making it a favorite among data scientists and analysts.Why U
4 min read
Create Grouped Bar Chart using Altair in Python Grouped bar charts are a handy tool to represent our data when we want to compare multiple sets of data items one against another. To make a grouped bar chart, we require at least three rows of three columns of data in our dataset. The three columns can be used as- one for values, one for series, an
3 min read
Scatter Plot with Regression Line using Altair in Python Prerequisite: Altair In this article, we are going to discuss how to plot to scatter plots with a regression line using the Altair library. Scatter Plot and Regression Line The values of two different numeric variables is represented by dots or circle in Scatter Plot. Scatter Plot is also known as a
4 min read
Horizontal Stripplot with Jitter using Altair in Python Prerequisites: Altair Altair is a statistical data visualization library in python which is based on Vega and Vega-Lite visualization grammars. A Stripplot is used for graphical data analysis. Â It is a simple plot of response values in a sorted order along a single axis. The strip plot consists of 2
2 min read
How To Make Stripplot with Jitter in Altair Python? Prerequisites: Altair Altair is a statistical data visualization library in python which is based on Vega and Vega-Lite visualization grammars. A Stripplot is used for graphical data analysis. Â It is a simple plot of response values in a sorted order along a single axis. The strip plot consists of 2
3 min read
How To Facet a Scatter Plot with Altair? In this article, we will learn how to Facet a Scatter Plot with Altair. Let's recall some concepts : Altair is a statistical visualization library in Python. It is declarative in nature and is based on Vega and Vega-Lite visualization grammars. It is fast becoming the first choice of people looking
3 min read
How to Make a Simple Histogram with Altair in Python? Prerequisites: Altair Simple Histogram is the representation of frequency distribution by means of rectangles whose width represents the class interval. Histogram is the graphical representation that organizes grouped data points into the specified range. By using histogram we can visualize large am
4 min read
Area Chart with Altair in Python Prerequisite: Introduction to Altair in Python An Area Graph shows the change in a quantitative quantity with respect to some other variable. It is simply a line chart where the area under the curve is colored/shaded. It is best used to visualize trends over a period of time, where you want to see h
2 min read
How to Make Overlapping Histograms in Python with Altair? Prerequisite: Altair A histogram represents data provided during a sort of some groups. It is an accurate method for the graphical representation of numerical data distribution. It is a kind of bar plot where the X-axis represents the bin ranges while the Y-axis gives information about frequency. Us
2 min read
Python Altair - Scatter Plot In this article, we will learn a Simple Scatter plot with Altair using python. Altair is one of the latest interactive data visualizations library in python. Altair is based on vega and vegalite- A grammar of interactive graphics. Â Here we will import the Altair library for using it. And then we wil
2 min read