How to draw a bar chart in Altair

Altair, a Python-based declarative statistical visualization library, offers a streamlined approach to crafting diverse visualizations. Its purpose is to facilitate the creation of expressive and informative charts, enabling users to generate impactful visual representations without delving into intricate plotting code. Altair contributes to the extensive range of data science and visualization tools available within the Python ecosystem.

The bar chart in Altair

A bar chart, also known as a bar graph, represents categorical data using rectangular bars. These bars visually depict the values they represent, with their lengths directly proportional to the data they convey. Bar charts are effective tools for visualizing and comparing data across categories or groups. Based on preference and data layout, bars can be arranged horizontally or vertically.

To draw a bar 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 the data into a pandas DataFrame
data = pd.read_csv('your_data.csv')
Importing the dataset in our code
  1. Creating a bar chart: We use the Altair API to create a bar chart. We specify the data source, encoding channels (x and y axes), and mark type (in this case, bar).

chart = alt.Chart(data).mark_bar().encode(
x='Category',
y='Value'
)
Creating the bar chart
  1. Customizing the chart: We can add various customizations to our bar charts, such as axis labels, titles, tooltips, and color schemes.

Example

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

import altair as alt
import pandas as pd
import os
# Hypothetical data: Average ratings of programming languages
data = pd.DataFrame({
'Language': ['Python', 'JavaScript', 'Java', 'C++', 'Ruby'],
'AverageRating': [4.5, 4.2, 3.8, 3.5, 4.0]
})
# Create an interactive vertical bar chart
chart = alt.Chart(data).mark_bar().encode(
x=alt.X('Language:N', title='Programming Language'),
y=alt.Y('AverageRating:Q', title='Average Rating'),
color=alt.Color('Language:N', legend=None),
tooltip=['Language', 'AverageRating']
).properties(
title='Average Ratings of Programming Languages',
width=400
)
# Display the chart
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 Language and AverageRating columns.

  • Lines 11–20: We initialize an Altair chart with data. We specify that it’s a bar chart (mark_bar) and maps Language to the x-axis and AverageRating to the y-axis. We set the chart color, tooltip, title, and width.

  • 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 ©2025 Educative, Inc. All rights reserved