A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/introduction-to-altair-in-python/ below:

Introduction to Altair in Python

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:

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:

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:

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 Altair

Explanation: 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 Altair

Explanation: 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 Altair

Explanation: 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 Altair

Explanation: 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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4