Altair is a statistical visualization library for Python. Its syntax is clean and easy to understand as we will see in the examples. It is also very simple to create interactive visualizations with Altair.
In the first part of the Altair series, we created some basic plots to get familiar with the syntax and structure. It was kind of an introductory guide for Altair. In the second part, we focused more on the filtering and data transformation in the visualizations.
Note: Here is a list of the articles in Altair series.
- Part 1: Introduction
- Part 2: Filtering and transforming data
- Part 3: Interactive plots and dynamic filtering (This article)
- Part 4: Customizing visualizations
In this article, we will elaborate on interactive visualization. Altair makes it quite efficient to create interactive visualizations. It also allows for using other plots, legends, and some widgets as a filtering method.
We start by importing Altair. If you are using Google Colab, it is already installed and can be imported directly. If not, you can easily install it using pip.
import altair as alt
We will do the examples based on a customer churn dataset which is available on Kaggle.
cols = ['Attrition_Flag','Gender','Education_Level', 'Marital_Status','Credit_Limit','Total_Trans_Amt','Total_Trans_Ct']
churn = pd.read_csv("/content/BankChurners.csv", usecols=cols)
.sample(n=1000)
churn.head()

I have created the dataframe with a sample of 1000 data points (i.e. rows) from the original datasets. I have selected only the columns that will be used in the visualizations.
Example 1
The first example consists of a scatter plot and a bar plot. We will bind these plots in a way that the bar plot is updated according to the selected part of the scatter plot.
We first create a selection object using the selection function of Altair. The selection object is what makes the plots interactive in this case.
selection = alt.selection(type='interval')
We can now create the scatter plot and bar plot.
plt1 = (alt.
Chart(churn).
mark_circle(size=50).
encode(
x='Credit_Limit', y='Total_Trans_Amt',
color = alt.condition(selection, 'Gender',
alt.value('lightgray'))
).
add_selection(selection))
plt2 = (alt.
Chart(churn).
mark_bar().
encode(y='Gender', x='count(Gender):Q',color = 'Gender').
transform_filter(selection))
The syntax starts with a top-level Chart object. In the second line, we specify the type of plot. We specify what to plot in the encode function. The columns to be used in x and y axes, as well as the separating color column, are specified in this function.
There is an important difference between these two plots. It is the way we pass the selection object.
In the first plot, the selection object is passed as a selection filter whereas it is a transformation filter for the second plot. Thus, the second plot is updated based on the selected part in the first plot.
We now need to plot them together. We can either use the concat functions of Altair or concatenation operators.
plt1 & plt2

The scatter plot displays the credit limit and total transaction amount columns. The bar plot that shows the number of males and females is updated as we select different sections on the scatter plot.
Example 2
Let’s do the second example in kind of an opposite way of the first example. We will use the bar plot to update the scatter plot.
selection = alt.selection(type='interval')
plt1 = (alt.
Chart().
mark_circle(size=50).
encode(x='Credit_Limit', y='Total_Trans_Amt',
color='Gender').
transform_filter(selection))
plt2 = (alt.
Chart().
mark_bar().
encode(
x='Marital_Status', y='mean(Total_Trans_Amt):Q',
color=alt.condition(selection, alt.value("lightblue"),
alt.value("lightgray"))
).
properties(height=300, width=200).
add_selection(selection))
The first plot is a scatter plot that displays the credit limit and total transaction amount columns. We pass the gender column to the color parameter to separate data points according to gender. Unlike the scatter plot in the previous example, the selection object is added with the transform_filter function because the scatter plot will be updated based on what is selected in the second plot.
The second plot is a bar plot that shows the average total transaction amount for different categories in the marital status column.
As you can see, data transformation is pretty straightforward in Altair. We can pass the desired transformation as below:
y='mean(Total_Trans_Amt):Q'
After the encode function, we use the properties function can adjust the size of plots. Finally, the selection object is added with the add_selection function.
You may have noticed that we did not pass the name of the dataframe to the Chart function in this example. The reason is the way we will concatenate these plots.
alt.hconcat(plt1, plt2, data=churn)
The dataframe can be passed to the data parameter of the concatenate functions (hconcat or vconcat).

The scatter plot is updated based on the selected sections in the bar plot. The y axis of the bar plot shows the average total transaction amount for different categories of the marital status column.
Example 3
One of the nice features of Altair is that the legends can be used as a filter. In this example, we will create a scatter plot that can be updated using the legend.
We start by creating the selection object as usual.
selection = alt.selection_multi(fields=['Education_Level'], bind='legend')
We pass the name of the columns that will be used as the legend and filtering option.
(alt.
Chart(churn).
mark_circle(size=50).
encode(
x='Total_Trans_Ct', y='Total_Trans_Amt',
color= alt.Color('Education_Level:N',
scale=alt.Scale(scheme='category20b')),
opacity=alt.condition(selection, alt.value(1), alt.value(0.1))
).
properties(height=400, width=500).
add_selection(selection))

The data points displayed on the scatter plot are updated based on the selected category in the legend.
Conclusion
In the first part of the Altair series, we created some basic plots to get familiar with the syntax and structure. It was kind of an introductory guide for Altair. In the second part, we focused more on the filtering and data transformation in the visualizations.
In this article, we have created interactive plots and see how selection objects can be used to add interactive features to the plots.
There is still much more this library offers. I will be writing more tutorials about Altair. Stay tuned for more advanced features of this library.
Thank you for reading. Please let me know if you have any feedback.