How to draw a line chart in Altair

Altair is a Python library for declarative data visualization. It provides a high-level interface for creating interactive and informative visualizations from data. Altair is built on top of Vega and Vega-Lite, which are visualization grammars designed to make it easier to generate visualizations in a consistent and concise manner.

Line chart in Altair

In Altair, a line chart is a type of data visualization that represents data points as connected line segments. It is often used to display how a particular variable changes over a continuous or ordinal range of values, typically along the x-axis. Line charts are especially useful for showing trends, patterns, and changes in data over time or across ordered categories.

To draw a line chart in Altair, we’ll need to follow these general steps:

  1. Importing Altair: We import the Altair library in our Python code. We need a dataset to visualize. We can use pandas or other data manipulation libraries to load our data.

import altair as alt
import pandas as pd
# Load your data into a Pandas DataFrame
data = pd.read_csv('your_data.csv')
Importing dataset in our code
  1. Creating an Altair chart: We use the alt.Chart function to create the base chart object. We pass our data to this function.

chart = alt.Chart(data)
Creating the chart object
  1. Encoding the data: For a line chart, we typically map our x-values to the x-axis and our y-values to the y-axis. We can also customize the line style, color, and other visual attributes.

line = chart.mark_line().encode(
x='x_values',
y='y_values'
)
Encoding data
  1. Customizing the chart: We can add various customizations to our line charts, such as axis labels, titles, tooltips, and color schemes.

line = line.properties(
title='Line Chart Example',
).encode(
tooltip=['x_values', 'y_values'],
)
Adding chart customizations

Example

Let’s create a basic line chart in Altair using sample data.

import altair as alt
import pandas as pd
import os
# Create a sample DataFrame
data = pd.DataFrame({
'x_values': [1, 2, 3, 4, 5],
'y_values': [10, 20, 15, 30, 25]
})
chart = alt.Chart(data).mark_line().encode(
x='x_values',
y='y_values'
).properties(
title='Line Chart',
).encode(
tooltip=['x_values', 'y_values'],
)
chart.save('chart.html')
os.system('cat chart.html')

Explanation

  • Lines 1–3: We import Altair and the necessary libraries.

  • Lines 5–9: We create a pandas DataFrame named data with x_values and y_values columns.

  • Lines 11–18: We initialize an Altair chart with data. We specify that it’s a line chart (mark_line) and maps x_values to the x-axis and y_values to the y-axis. We set the chart title and add tooltips.

  • Line 20: We save the chart using chart.save('chart.html'). It exports the chart to an HTML file named chart.html.

  • Line 21: We display the chart on the console.

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