Data visualization using the Python Altair library
Altair is a Python declarative visualization library. It is a straightforward, user-friendly, and uniform API built on top of powerful visualization grammars such as
A declarative visualization library is a library for which, instead of specifying how to create a step-by-step visualization, we declare what we want to visualize and let the library handle all the details. On the other hand, in an imperative visualization library, we have to specify the exact steps and instructions to be followed to create desired visualizations.
Using Altair, we can create many data visualizations, such as bar charts, grid plots, histograms, bubble charts, etc. Here, we will discuss the steps of creating charts using Altair.
Installing dependencies
Altair is not a standalone library because it is dependent upon several base libraries. To install all the dependencies from PyPi (Python Package Index), a software repository for Python, use the following command:
pip3 install pandas seaborn altair
We install pandas for data manipulation to create charts and the seaborn to import datasets.
Importing libraries
Data visualization is mostly done via Jupyter Notebooks. Hence, we will create a Notebook called the altair.ipynb and in the first code tab, import the required libraries.
import altair as altimport seaborn as snsimport pandas as pdfrom IPython.display import display
Lines 1–3: Import the
altairlibrary asalt,seabornassns, andpandaslibrary aspdin the DataFrame.Line 4: Import the
displaymethod from theIPython.displayto display charts in the Jupyter Notebook.
Load data
Here, we are going to use the "iris" dataset provided by the Seaborn library. The attributes of the iris dataset is explained as follows:
Petal length (in centimeters): Is the iris flower’s petal length, which is the inner and colorful part of the flower.
Petal width (in centimeters): Is the iris flower’s petal width.
Sepal length (in centimeters): Is the iris flower’s sepal length, which is the outermost and protective structure of the flower.
Sepal width (in centimeters): Is the iris flower’s sepal width.
Species: Is the label to represent the species of the flower. The categories are “setosa,” “versicolor,” and “virginica.”
To load the dataset in the Notebook, use the following command:
df = sns.load_dataset("iris")print(df.head())
Line 1: Load dataset from the Seaborn into the variable named the
df.Line 2: Print the header of the dataset using the
head()method. Since there is no specific number mentioned, thehead()method will return first five rows of the data by default.
Create an Altair chart
We’ll create a basic scatter plot using Altair, where we will map the sepal_length to the x-axis and sepal_width to the y-axis. We’ll also use the species as the color encoding, which is a method to represent numbers as colors.
# Create an Altair scatter plotchart = alt.Chart(df).mark_circle(size=60).encode(x='sepal_length:Q',y='sepal_width:Q',color='species:N',tooltip=['sepal_length:Q', 'sepal_width:Q', 'species:N']).properties(title='Iris Sepal Length vs. Width',width=500,height=300)# Display the chart using IPython's display functiondisplay(chart)
Code explanation
alt.Chart(df): This initializes a base Altair chart using thedfas the data reference..mark_circle(size=60): This sets the mark type for the chart circles and sets the circle size to60..encode(...): This determines that the variables will be mapped as:x='sepal_length:Q': This maps thesepal_lengthcolumn to x-axis, implying that it is a quantitative variable.y='sepal_width:Q': This maps thesepal_widthcolumn to y-axis and shows that it is also a quantitative variable.color='species:N': This maps thespeciescolumn to color encoding and indicates that it is a categorical variable.tooltip=['sepal_length:Q', 'sepal_width:Q', 'species:N']: This shows the data variables that will be displayed in the tooltip when we hover over the circles.
.properties(...): This sets several chart properties, such as width, height, and title.display(chart): This renders the chart and displays it in the Notebook.
By clicking the “Run” button below, we can practice creating a chart using the Altair library.
import React from 'react';
require('./style.css');
import ReactDOM from 'react-dom';
import App from './app.js';
ReactDOM.render(
<App />,
document.getElementById('root')
);
In the Jupyter Notebook, we can see the different steps of making a scatter plot using Altair library. We import libraries, load a dataset, declare values for visualization, and display the scatterplot. Any Altair chart can be created using the similar steps mentioned above.
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:
Data visualization using the Python Altair library
Get an introduction to Altair, its purpose, installation, and basic usage for data visualization.What are the main elements of an Altair chart?
Learn about the key components that make up an Altair chart and how they contribute to creating meaningful visualizations.How to implement encoding in Altair
Understand how encoding is used to map data to visual properties.How to draw a line chart in Altair
Discover how to create a simple yet effective line chart using Altair.How to draw a bar chart in Altair
Understand how to create bar charts in Altair for comparing categories and visualizing data values.How to draw a scatter plot in Altair
Explore the process of creating scatter plots in Altair to visualize relationships between variables.How to draw a box plot in Altair
Learn how to create box plots in Altair for displaying the distribution of data through quartiles.How to draw a heatmap in Altair
Discover how to create heatmaps in Altair to represent data intensity and patterns using color coding.How to draw a stacked area chart in Altair
Understand how to create stacked area charts in Altair to visualize cumulative data over time.How to draw a geographical map in Altair
Learn how to visualize geographical data and create interactive maps using Altair’s geospatial capabilities.How to draw a pie chart in Altair
Discover how to create pie charts in Altair, ideal for visualizing proportions of a whole.
Free Resources