Python Tutorial: Legends, annotations, & styles

Learn to add legends with the label argument and legend() function, place text annotations with arrows using annotate(), and switch global styles with style.use(). The tutorial uses the Iris dataset and compares ggplot and 538 style sheets.
Chapters
Let's look at a few more tricks to make plots fancier. Legends are useful when several kinds of plots or curves are displayed on the same axes. A legend is a box with labels to help readers distinguish distinct features. Here, the famous iris dataset is plotted, showing the sepal width versus the sepal length for three species of flower, Setosa, versicolor, and Virginica. Here is some code that generates the previous plot. Notice that each scatterplot is given a label to use in the legend, and the legend is created with the legend command. The keyword argument loc specifies where to put the legend. The location of the legend can be given as a specific string, like lower left or using an equivalent integer code. It's usually easier to remember the string than the numeric location code, even if it's more verbose. The annotate function adds text to a figure. It can also draw arrows from the text to some other features to highlight. There are flexible ways to specify coordinates in the API. There's also a keyword argument arrow props that uses a Python dictionary to customize the arrow that's drawn. Let's avoid the legends this time and place text labels on the plot directly. With the annotate function, we'll place directly at locations given by the tuple XY in each invocation. This is the resulting figure. The annotate function requires at least the string argument s for the text to draw. The keyword argument XY tells the point being annotated. If we also want to draw an arrow, we need to specify the coordinates of the text and the point being annotated to draw the arrow. The keyword arrow props needs to be specified by a dictionary with the arrow's properties. And here's the same figure, this time drawing arrows from the text labels to the points being annotated. This is the required code. Notice the Specification of coordinates XY and XYText in the calls to annotate. It usually takes some experimentation to get the coordinates to make a visually pleasing plot. The dictionary arrow props is required to make the arrow appear. Finally, we should know that the default plot styles are controlled by style sheets. In matplotlib, the stylesheets control default fonts, line widths, color palettes, backgrounds, and more. We can switch between global style sheets using style use, and we can find out what styles we can use with style available. For instance, here's a sample of our plot created with the ggplot style sheet, and here's the same plot drawn using the 538 style sheet. Now it's your turn to practice working with legends, annotations and plot styles.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







