Get startedGet started for free

Adding text to plots

1. Adding labels and legends

In the previous video, we learned how to create a line plot using plt-dot-plot and how to display the plot using plt-dot-show. In this lesson, we'll explain our plot to potential viewers by adding labels and legends.

2. What did we just plot?

Previously, we plotted the frequency of each letter in the ransom note found at the scene of the kidnapping of Bayes. However, if we showed this plot to a judge without any context, she wouldn't know what we were trying to communicate. When we share our graphs with an audience, we must add labels to make it clear what we've plotted and why it's important.

3. Axes and title labels

First, let's add some axis labels. We can label the horizontal (or "x") axis using the command plt-dot-xlabel. This function takes one argument: a string that represents what we want the label to read. Similarly, we can label the vertical (or "y") axis using the command plt-dot-ylabel. Finally, we can give our plot a title by using plt-dot-title. With all three functions, it's important to remember that the argument must be a string. If we forget to put our string in quotes, we'll get a syntax error. We can add these labeling functions in any order and at any place in our code as long as they come before plt-dot-show.

4. Legends

If we have multiple lines in the same plot, we'll want to add a legend. There are two steps for adding a legend: First, we must add the keyword argument label to each instance of plt-dot-plot. The label will be a string that we want in our legend. By adding a label to each plt-dot-plot function, we make our code easier to read. Now, if someone looks at this code, they know what each line is plotting before ever looking at the result. If we just type plt-dot-show now, we still won't see a legend. In order to add a legend, we must add a final function: plt-dot-legend. Like plt-dot-show, plt-dot-legend does not take any arguments. It just tells Matplotlib to use the labels from our plt-dot-plot functions to create a legend.

5. Arbitrary text

Sometimes, we just want to add a quick note directly onto our plot. We can do this by using the function plt-dot-text. This function takes three arguments: the x-coordinate where we want to put the text, the y-coordinate where we want to put the text, and the text we want to display as a string. Suppose we wanted to add a note to our ransom diagram? If we want the note to appear at the coordinated (5, 9), we can use this command, to produce this image.

6. Modifying text

Whether we're adding labels, legends, or arbitrary text, we might want to modify the letters in some way. There are two easy keyword arguments that we can add to any of our functions to change the way that text is displayed. If we want to change the size of font, we can use the keyword argument fontsize and pass in a float. If we want to change the color of font, we can use the keyword argument color and pass in a string. For a list of allowed color names, we can visit Wikipedia and look up "web colors".

7. Let's practice!

Great job! Let's return to the data from Officer Deshaun and his friends, and add some labels.