Get startedGet started for free

Adding titles and labels: Part 2

1. Adding titles and labels: Part 2

Hello! In this lesson, we'll continue learning how to customize plot titles and axis labels.

2. Adding a title to AxesSubplot

In the last lesson, we learned how to add a title to a FacetGrid object using "g dot fig dot suptitle". To add a title to an AxesSubplot object like that from the "box plot" function, assign the plot to a variable and use “g dot set_title”. You can also use the “y” parameter here to adjust the height of the title.

3. Titles for subplots

Now let's look at what happens if the figure has subplots. Let's say we've divided countries into two groups - group one and group two - and we've set "col" equal to "Group" to create a subplot for each group.

4. Titles for subplots

Since g is a FacetGrid object, using "g dot fig dot suptitle" will add a title to the figure as a whole.

5. Titles for subplots

To alter the subplot titles, use "g dot set_titles" to set the titles for each AxesSubplot. If you want to use the variable name in the title, you can use "col name" in braces to reference the column value. Here, we've created subplot titles that display as "this is group 2" and "this is group 1".

6. Adding axis labels

To add axis labels, assign the plot to a variable and then call the "set" function. Set the parameters "x label" and "y label" to set the desired x-axis and y-axis labels, respectively. This works with both FacetGrid and AxesSubplot objects.

7. Rotating x-axis tick labels

Sometimes, like in the example we've seen in this lesson, your tick labels may overlap, making it hard to interpret the plot. One way to address this is by rotating the tick labels. To do this, we don't call a function on the plot object itself. Instead, after we create the plot, we call the matplotlib function "plt dot xticks" and set "rotation" equal to 90 degrees. This works with both FacetGrid and AxesSubplot objects.

8. Let's practice!

And that's it! Now it's time to create some clear and informative visualizations!