Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Faceting Charts
00:00
In the last lesson, you added more encoding channels to map more of your data to the final visualization. In this lesson, you will change the .encode() method so that multiple charts are created at once, using something called faceting.
00:15 Faceting means separating your charts by a specific variable. For example, creating one chart for each penguin species. If you were using Matplotlib, which uses an imperative approach, you’d have to create those charts separately by creating separate objects, which just requires a bit more code.
00:33
In Altair, you can change the .encode() method to automatically facet your charts by a specific variable. In the code, you can add a column parameter to the .encode() method and pass in the column that you want to use for the individual facets.
00:49
In this case, you want one chart per species, so the value of the column parameter will be Species, with colon capital N at the end because it’s a nominal category.
00:59 When you run this code, you automatically get one chart per penguin species. Now, as you can see in this output, because we have already colored the points by species, each separate chart has only one color on it, which isn’t very useful information.
01:15
So if you go back into the code, you can change the column that is mapped to the color encoding channel, and let’s use the penguin’s sex. So for each chart, we should see each data point colored by whether the penguin in question is male or female, according to the data.
01:32 When you run the code, you can see now the data points are colored by the penguin’s sex. The other reason this is useful is it highlights data quality issues, such as penguins that have a period or just a null value for their sex.
01:48
So in this lesson, you learned how to change the .encode() method to create facets, which are multiple charts based on one of your columns in the data.
01:57 In the next lesson, you’ll add more interactivity beyond the hovering tooltips.
Become a Member to join the conversation.