Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding Interactivity

00:00 In the last lesson, you learned how to split your charts by a single column using faceting, and in this lesson, you’re going to add more interactivity to your charts.

00:09 Because the focus of Altair is on exploratory data analysis, user interactions are a key part of using Altair. Beyond controlling tooltips when you hover over your data, you can capture user input like clicks and drags with a selection, then you specify a series of when and then steps using the .when() and .then() methods to define the behavior of what happens to the chart when the user interacts with it.

00:35 This can lead to much more rich behavior than just hovering over and getting a tooltip. The first step to this kind of interactivity is to capture an interval.

00:45 While this could be different kinds of interactivity, we’re going to use the interval as an example, which is when you drag a rectangle around certain data points on your chart.

00:55 To do this, you’re going to create a parameter object using a method that starts with the word selection. Let’s start this one from scratch in a new cell.

01:06 Create an object called brush equals alt.selection_interval().

01:13 In another cell, let’s just check the type of this new variable, which is actually a Parameter. This is the key to interactivity with Altair.

01:23 The next step is to create a chart the same way you did before using the data, mark, encoding process.

01:31 So in this code cell, you’re going to repeat the process from before. alt.Chart(), passing in the DataFrame, then the .mark_point() method to say it’s going to be a scatter plot, and then the .encode() method to map your data to your visual elements.

01:48 x is going to equal the Body Mass column in grams, using the :Q annotation because it’s a numeric column.

01:57 y is going to be Flipper Length, which is in millimeters as per the column name, and also using the :Q annotation. Let’s also use the same approach from before, which is putting the whole thing in parentheses and laying out the code a bit more neatly.

02:17 Let’s scroll down to actually see all of the code.

02:25 And the other change you’re going to make is saving this chart into a variable. We’re going to call it scatter to remind us that it’s a scatter plot.

02:34 So as a reminder, step one was to create this parameter object. Step two was to create this chart. And step three is to add the interactivity within the .encode() method.

02:47 The specific interactivity we’re going to add is when you drag a rectangle around data points, they become highlighted based on the penguin species. Otherwise, they’re going to stay gray.

02:59 You’re going to add these rules to the encoding channel using the .when(), .then(), and .otherwise() methods.

03:06 So inside the .encode() method, color is going to equal not just a column name, but a series of method calls. So it’s going to look like this.

03:16 Let’s put it in parentheses again to lay it out neatly. alt.when() to tell Altair under what conditions you’d like these rules to occur. And here we pass in this parameter object, which tells Altair what happens when a data point is within this selection interval.

03:33 Next, add a .then() method, which tells Altair what to use as the value for color when a data point falls within this selection interval. So here the color is going to be based on species like it did in the previous chart, except this time this only happens when it’s within this selection interval.

03:53 Finally, add an .otherwise() method at the end and add the Altair value "lightgray", which tells Altair what happens when a data point falls outside of this selection interval.

04:05 It’s almost like a series of if-else rules.

04:10 The final step is then to make sure that the selection object is linked to the chart by using the .add_params() method, which links the brush object, which is a parameter object, to the chart.

04:24 This happens after the .encode() method.

04:27 Down here, .add_params(), and pass in the object, which is a parameter object that we called brush. Now if you run this code, nothing happens because we saved the output into a variable called scatter, and our Jupyter Notebook no longer shows that scatter variable.

04:46 So just down here, we’re going to say scatter, which then forces Jupyter to actually render the chart object. And if you scroll down, you see that when the interaction occurs, when I start dragging this rectangle, data points that fall inside this, what we call the selection interval, become colored by species, otherwise they stay light gray, which is exactly the set of rules that we specified for Altair.

05:14 Here again, you can see the entire code snippet. The main difference from before is creating a parameter object, saving it into a variable which we called brush, changing the color encoding channel from a single column name to a series of when-then rules, and finally using the .add_params() method to link this parameter object, which we called brush, to our chart.

05:37 Now that you know how to add interactivity to your charts, in the next lesson, you will learn how to link two charts together. When you interact with one chart, the other one will also update.

Become a Member to join the conversation.