Python Tutorial: Legends, annotations, & styles

Published: 01 March 2020
on channel: DataCamp
1,027
8

Want to learn more? Take the full course at https://learn.datacamp.com/courses/in... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---

Let's look at a few more tricks to make plots fancier.

Legends are useful when several kinds of plots or curves are displayed in the same axes.

A legend is a box with labels to help readers distinguish distinct features.

Here, the famous iris data set is plotted showing the sepal width versus the sepal length for three species of flower, setosa, versicolor, and virginica.

Here is some code that generatez the previous plot.

Notice that each scatter plot is given a label to use in the legend.

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 is more verbose.

The annotate() function adds text to a figure.

It can also draw arrows from the text to some feature to highlight.

There are flexible ways to specify coordinates in the API.

There is also a keyword argument arrowprops that uses a Python dict to customize the arrow drawn.

Let's avoid the legend 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 resulting figure.

The annotate function requires at least the string agrument s for the text.

The keyword argument xy tells the point being annotated.

If we want to draw an arrow, we need to specify the coordinates xytext of the text and the point being annotated.

To draw the arrow, the keyword arrowprops needs to be specified by a dict with the arrow's properties.

This is 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 callse to annotate().

It usually takes some experimentation to get the coordinates to make a visually pleasing plot.

The dict arrowprops is required to make the arrow appear.

Finally, we should know that the default plot styles are controlled by style sheets in Matplotlib.

The style sheets 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 five-thirty-eight style sheet.

Now it's your turn to practice working with legends, annotations, and plot styles.


On this page of the site you can watch the video online Python Tutorial: Legends, annotations, & styles with a duration of hours minute second in good quality, which was uploaded by the user DataCamp 01 March 2020, share the link with friends and acquaintances, this video has already been watched 1,027 times on youtube and it was liked by 8 viewers. Enjoy your viewing!