What are the main elements of an Altair chart?

The Altair library is used for declarative statistical visualization in Python. It’s designed to simplify the creation of interactive and informative visualizations for data analysis. Altair is built on top of the Vega and Vega-Lite visualization grammars, which are frameworks for creating visualizations using a high-level, declarative syntax. There are two types of visualization libraries:

  • Imperative libraries: They focus on how to build a visualization, such as specifying the steps manually to build the visualization (axis, size, legend, labels). Matplotlib is an example of an imperative library.

  • Declarative libraries: They focus on what we want to see. We specify the data and the type of visualization we want to see. The library will do the manipulations to create the visualization for us automatically. Altair is an example of a declarative library.

The main elements of an Altair chart

Every Altair chart comprises three main elements: data, chart, mark, and encodings.

Data

The dataset is a fundamental element in Altair charts. We need a dataset to visualize. We can use pandas or other data manipulation libraries to load our data.

import pandas as pd
# Load your data into a pandas DataFrame
data = pd.read_csv('your_data.csv')
Importing a dataset

Chart

A chart is the entry point element in Altair. Every Altair chart receives an input of a single argument, which is the dataset.

To start creating a chart, we can import the Altair library and then create the chart.

import altair as alt
alt.Chart(dataset)
Creating a chart

The dataset can be in one of the following formats:

  • pandas DataFrame

  • Data or related object (i.e., UrlData, InlineData, NamedData)

  • URL pointing to a JSON or CSV file

  • Object supporting the geo_interface (e.g., Geopandas GeoDataFrame, and so on)

Mark

A mark is a chart property that defines how to represent data. Examples of marks include bar charts, line charts, area charts, and many more. To specify a mark, we append it to the chart.

alt.Chart(dataset).mark_bar()
Representing data through mark

The above example shows how to use Altair to draw a bar chart. In general, the name of each mark is mark_<type_of_graph>().

Encodings

Encodings specify where to represent data, including their position, size, color, etc. To define an encoding, we append the encode() property to the chart.

alt.Chart(dataset).mark_bar().encode()
Encoding the data

Example

Let’s consider the Christmas trees dataset.

Year

RealTree

FakeTree

2004

27100000

9000000

...

...

...

2016

27400000

18600000

Let’s draw a simple line chart in Altair.

import altair as alt
import pandas as pd
import os
df = pd.read_csv('/christmas_trees.csv')
chart = alt.Chart(df).mark_line().encode(
x = 'Year:O', # O for ordinal data
y = 'RealTree:Q' # Q for quantitative data
)
chart.save('chart.html')
os.system('cat chart.html')

Explanation

We load the dataset as a pandas DataFrame and then draw a simple line chart in altair of the RealTree column versus the Year column.

We use the mark_line() property to draw a line and specify the x and y axes in the encode() property. For each column, we must also select the type (O for Year and Q for RealTree). We use the last two statements of the code to render the chart.

Unlock your potential: Data visualization with the Altair series, all in one place!

To continue your exploration of data visualization using the Altair library, check out our series of Answers below:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved