How to draw a pie chart in Altair

Altair, a Python-based declarative statistical visualization library, offers a streamlined approach to crafting diverse visualizations. It is designed to simplify the creation of a wide range of visualizations, making it easy for users to generate expressive and informative charts without the need for low-level plotting code. Altair is part of the broader ecosystem of tools for data science and visualization in Python.

The pie chart in Altair

A pie chart, often called a circular statistical graphic, visually segments a whole into proportional slices to showcase numerical proportions. Each segment within the pie chart symbolizes a portion of the total dataset, with the magnitude of each slice corresponding to its respective quantity. The entirety of the circle encapsulates the entirety of the data, representing a complete dataset.

Pie charts are commonly used to show the composition of a whole, especially when there are a small number of categories. They effectively display relative proportions and provide a quick visual overview of how parts contribute to the whole.

Creating a pie chart in Altair is similar to creating other charts. Here are the steps to draw a pie chart using 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 the dataset in our code
  1. Creating a pie chart: We use the Altair API to create a pie chart. We specify the data source, encoding channels, and mark type (in this case, arc).

chart = alt.Chart(data).mark_arc().encode(
color='Category:N',
theta='Value:Q',
tooltip=['Category', 'Value']
).properties(
width=300,
height=300
)
Creating the pie chart
  1. Customizing the chart: We can add various customizations to our pie charts, such as labels, titles, tooltips, and color schemes.

Example

Let's create a meaningful example using a hypothetical dataset representing the distribution of expenses in a monthly budget. This pie chart will show the proportions of spending in different expense categories

import altair as alt
import pandas as pd
import os
# Hypothetical data: Monthly budget expenses
data = pd.DataFrame({
'Category': ['Housing', 'Transportation', 'Food', 'Entertainment', 'Utilities'],
'Expense': [1200, 400, 300, 200, 150]
})
# Create a pie chart
chart = alt.Chart(data).mark_arc().encode(
color='Category:N',
theta='Expense:Q',
tooltip=['Category', 'Expense']
).properties(
title='Monthly Budget Expenses',
width=400,
height=400
)
# Display the chart
chart.save('chart.html')
os.system('cat chart.html')

Explanation

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

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

  • Lines 11–20: We initialize an Altair chart with data where:

    • Category variable (Nominal N) determines the color of arcs

    • Value variable (Quantitative Q) sets the angle of arcs

    • tooltip displays both category and value information

    • Chart dimensions are set to 400x400 pixels

N and Q are shorthand notations for data types in Altair, which represent nominal (categorical) and quantitative (numerical) data types, respectively.

  • N (Nominal): Nominal data represents categories or groups without any inherent order or magnitude. Examples include names, labels, or categorical variables like colors, types, or species.

  • Q (Quantitative): Quantitative data represents numerical values with an inherent order and magnitude. Examples include integers, floats, or any numerical values that can be measured or compared.

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

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

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved