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

Linking Two Charts

00:00 In the last lesson, you learned about parameters and how to add interactivity to charts. In this lesson, you’ll create a second chart and link the two together with this interactivity.

00:11 To link two charts, first you need to create two charts. Then you can show them in the same output using the ampersand character. Once you have two charts, you can also link them using the same parameter object so that when one chart changes, the other one changes as well.

00:28 Let’s start by adding that second chart. You’re going to need this scatter object that you created in the previous lesson. In another cell, create a second chart.

00:38 This one is going to be a bar chart, which we will name bars. I’ll scroll down a bit so it’s a bit more visible. And this bars object is also going to be an alt.Chart(), passing in the same data.

00:52 And again, I’ve put this in parentheses so we can start laying it out a bit more neatly. So it’s going to be a chart passing in the data. Remember, the process is data, mark, encoding.

01:05 So the next step is the mark. And this time, it’s going to be .mark_bar(), which is an instruction to Altair that now we want a bar chart. The next step is then to use the .encode() method to map our data to these bars.

01:20 You’re going to create a bar chart, which is horizontal and shows the average flipper length per species. To do this, you’re going to actually have to add a calculation into this .encode() method.

01:32 But Altair supports that. On the x-axis, rather than just Flipper Length in millimeters, you’re actually going to use a mean() function, but inside that string, which is an instruction to Altair to take the average of the flipper length.

01:50 And right at the end, after the closing parenthesis of the mean() method, you can specify that the output here is also going to be a numeric column.

01:59 The average is also numeric. And then the y-axis is going to be the species, which is still nominal, hence :N.

02:09 Now if we use this bars variable, which stores our bar chart, and I’ll scroll down a bit so we can see, it has created the correct chart. On the x-axis is the mean of Flipper Length, and on the y-axis is one row or one bar per species.

02:26 To show both of these charts at the same time, you can say scatter & bars. And again, if I scroll down, you can see that both charts are now shown one above the other.

02:38 If you start interacting with the scatter plot, it obeys the same rules from before. Points inside the rectangle are colored, the rest are gray. However, nothing happens to the bottom bar chart when we do this.

02:51 This is the next step. If I scroll back up a bit to the code, you can make one small change to this code to make sure that the interactivity in one chart actually affects the other.

03:03 To do that, after the .encode() method, add a method called .transform_filter() and pass in the parameter, which we called brush.

03:12 This is an instruction to this chart to use the parameter that we specified before, which is linked to the scatter plot, to then also filter the data that goes into the bar charts.

03:24 If you run this, and we scroll down again to look at both charts at the same time, you’ll see that when you start selecting some of these data points, they filter the data that goes into the bar charts, and so the bar charts change to only show the data that is currently in the selection in the top scatter plot.

03:44 This way, we added interactivity to both charts by only specifying the interactivity in one and linking it to the other. So these two charts are now linked to each other.

03:55 Now currently, this chart only lives inside the Jupyter Notebook, but if you wanted to share this with someone, there is a way to do that by exporting it to HTML, which is the topic of the next lesson.

Become a Member to join the conversation.