How to draw a box plot in Altair

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

The box plot in Altair

A box plot is a graphical representation of the distribution of a continuous variable through its quartiles. It consists of a box representing the interquartile range (IQR) of the data, with a line inside representing the median. Additionally, it often includes whiskers extending from the box, representing the data range and excluding outliers. Box plots are useful for visually summarising the spread, central tendency, and skewness of the data and identifying potential outliers.

Example

We can use the mark_boxplot() function to draw a box plot in Altair. Here’s a basic example:

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 other necessary libraries.

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

  • Lines 11–19: We initialize an Altair chart with data. We specify that it’s a box plot (boxplot). We set the color of the boxes in the box plot to skyblue, size to 20, and opacity to 0.7. We map category to the x-axis and value to the y-axis. 'O' and 'Q' represent ordinal and quantitative data types respectively.

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

  • Line 23: 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 ©2026 Educative, Inc. All rights reserved