How to implement encoding in Altair

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Encoding in Altair

Here’s a step-by-step guide to implementing encoding in Altair:

  1. Importing Altair: We import the Altair library in our Python code.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Loading data: We need a dataset to visualize. We can use pandas or other data manipulation libraries to load our data.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Creating an Altair chart: We use the alt.Chart function to create the base chart object. We pass our data to this function.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  1. Defining encoding: The .encode() method maps data attributes to visual properties. We can chain the .encode() calls to specify multiple encodings. The syntax for encoding is as follows:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
  • x, y: Map data attributes for the x and y positions on the chart

  • color: Map data attributes for color

  • size: Map data attributes for size

  • tooltip: Map data attributes for the tooltip for interactivity (multiple attributes can be included in a list)

  1. Specifying chart type: We specify the type of chart we want to create by chaining a chart method, such as .mark_bar(), .mark_point(), .mark_line(), etc., to the chart object.

  2. Display the chart: Finally, we display the chart. There are different methods for rendering the chart for using Altair in a different environment.

Example

Let’s create a scatter plot with X values on the x-axis, Y values on the y-axis, and color-coded points based on the Color attribute.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

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

  • Lines 5–11: We create a pandas DataFrame named data with three columns: X, Y, and Color. It contains dummy data.

  • Lines 13–19: We initialize an Altair chart using the data DataFrame as the data source. We configure the chart with the .mark_point() method specifying that the chart should use points for data representation. We encode the data with .encode(x='X', y='Y', color='Color'). It defines how the data attributes should be visualized. In this case, X is mapped to the x-axis, Y to the y-axis, and Color determines the color of the points. The size channel maps a data attribute to the size of the visual elements (points). The tooltip encoding channel specifies the text that appears in a tooltip when we hover over a data point in the chart.

  • 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.

This code essentially creates a scatter plot using Altair, saves it as an HTML file, and then shows its content in 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 ©2026 Educative, Inc. All rights reserved