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 Encoding Channels

00:00 In the last lesson, you created your first Altair chart, and in this lesson, it’s time to add some more detail to it. So far, you mapped two pieces of information from your dataset to the visual, mapping two columns to the x- and y-axes respectively.

00:15 Now it’s time to add some more encoding channels. An encoding channel is the relationship between your data and a visual component, such as mapping a column to the x- or y-axes.

00:26 The next step is to add some color by mapping a third column of data to the final chart, and then adding a tooltip where you specify which columns of data you’d like to see when you hover over a point in the chart.

00:39 To do this, you’re going to add to the .encode() method. But first, as these commands are going to get quite long, it’s a good idea to lay out your code a little bit better.

00:48 If you add parentheses around this long statement, you can start adding newlines into your code without Python throwing an error message.

00:59 This will let you add to this code without it becoming too long horizontally.

01:06 Now you’ve done that, add a third encoding channel called color, and this time it will use the Species column. Now, the Species column is not numeric, it’s a nominal category, so you can use the capital N annotation instead.

01:23 Now when you run this code, you see that data points are colored by their penguin species. The other encoding channel we’re going to add is the tooltip. This time the tooltip is a list of column names.

01:37 So every time you hover over one of these penguin data points, it’s going to show the Species, the Body Mass in grams. I need to add that space. You need to take care to make sure these column names are exactly the same as in your data.

01:52 And finally, the Flipper Length,

01:56 also in millimeters, also using the capital Q annotation because it’s a quantitative numeric data column. Now when you run this code, and hover over individual data points, Altair shows a tooltip with the columns that you specified in the tooltip encoding channel.

02:18 So when specifying the tooltip, you didn’t need to write any additional code to make the interactivity work. It works out of the box by just adding the tooltip encoding channel.

02:29 So now you’ve seen a few ways to map data to your final visual. In the next lesson, you’re going to do something called faceting charts, which is to create multiple charts automatically from your data.

Become a Member to join the conversation.