How to Create Bindings and Conditions Between Multiple Plots Using Altair | by Khuyen Tran | Towards Data Science

Have you ever wanted to see one plot change when you interact with another plot like below? That is when Altair comes in handy.
The graph above is created with Altair. If you don’t know about Altair, Altair is a statistical visualization library for Python based on Vega and Vega-Lite. In my last article, I showed how Altair allows you to use concise visualization grammar to quickly build statistical graphics.
In this article, I will show how you can create bindings and conditions between multiple plots using Altair.
To install Altair, type:
pip install altair
We will use Altair to explore the CSV file downloaded from Students Performance in Exams data posted on Kaggle.
Cool! Now we have the data, let’s visualize it using Altair.
We will create a graph that uses an interval selection to filter the contents of a bar plot. Start with creating a scatter plot that allows us to select a specific interval by clicking and dragging.
Try to click and drag the chart above. You should see only the colors of the points inside the selected region are highlighted!
The code to produce the output above:
Explanations:
brush = alt.selection(type="interval") : Create interval selection object
encode : Specify columns shown in x-axis and y-axis
color=alt.condition(brush, “gender:N”, alt.value(“lightgray”) : If the points are in the selected interval, color those points by gender , else color those points lightgray .
tooltip : Information to show when hovering over a point
add_selection(brush) : Apply interval selection to the chart
Q specifies Quantitative (numerical data), N specifies Nominal (categorical data)
Refer to this article if you want to understand more about Altair’s basic syntax.
Now that we created the interval selection, let’s connect it to another bar plot.
Try clicking and dragging the scatter plot. You should see the distribution of the bar plot change when selecting different intervals!
Code to produce the plot above:
Explanations:
x="count(parental_level_of_education):Q": Count of each value in parental_level_of_education column.
color=alt.Color(“parental_level_of_education:N”) : Apply colors to bars based on parental_level_of_education
transform_filter(brush) : Filter data based on the selected interval
With Altair’s Bindings feature, you can do many cool things to bring clarity to your plot such as:
Sometimes you might wish to zoom in on your histogram to get a more granular view of your histogram. That can be done with Altair.
You can play with the chart yourself here:
Code to create the plot above:
You might also wish to understand the histogram by looking at which points fall in the selected interval of the histogram. That could also be done with Altair:
You can play with the chart yourself here:
Code to create the plot above:
You can also use Altair to apply multiple filters to your data points like below:
You can play with the plot here:
Code of the plot above:
I hope the code above doesn’t scare you! Many lines of the code above show what we already learned in the first section. Some new Altair’s objects that were not mentioned before:
alt.selection_single : Allow users to select only one option at a time
alt.binding_select : Create a dropdown bar
alt.binding_radio : Create a radio button
Congratulations! You have just learned how to bind multiple plots using Altair! Being able to link between multiple plots will bring much more clarity to your data analysis. Plus, you can impress your audience with these plots as well!
Feel free to play and fork the source code of this article here:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: