How to draw a scatter plot 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.

Scatter plot in Altair

In Altair, a scatter plot is a form of visualization depicting individual data points as markers on a 2D plane, where one variable is associated with the x-axis and another variable with the y-axis. Each marker denotes a distinct data point, and its position reflects the values of the respective variables.

Here's a basic example of how we might create a scatter plot in Altair:

  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. Creating scatter plot: We map our x to the x-axis and our y to the y-axis. We can also customize the line style, color, and other visual attributes.

# Create a scatter plot
scatter_plot = chart.mark_point().encode(
x='x',
y='y'
)
Creating scatter plot
  1. Customizing the chart: We can add various customizations to our scatter plots, such as axis labels, titles, tooltips, and color schemes.

scatter_plot = scatter_plot.properties(
title='Scatter Plot',
).encode(
tooltip=['x', 'y'],
)
Customizing the chart

Example

Let’s create a basic scatter plot in Altair using sample data.

import altair as alt
import pandas as pd
import os
# Sample data
data = pd.DataFrame({
'x': [2, 3, 6, 8, 10],
'y': [3, 6, 5, 11, 8]
})
# Create a scatter plot
scatter_plot = alt.Chart(data).mark_point().encode(
x='x',
y='y'
).properties(
title='Scatter Plot',
).encode(
tooltip=['x', 'y'],
)
scatter_plot.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 and y columns.

  • Lines 11–19: We initialize an Altair chart with data. We specify that it’s a scatter plot (mark_point) and maps x to the x-axis and y to the y-axis. We set the chart title and add tooltips.

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

  • Line 22: 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