Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Adding Legend | Plots Customization
course content

Зміст курсу

Ultimate Visualization with Python

Adding LegendAdding Legend

It is often the case when we have multiple elements on the canvas and it would be better to label them in order to describe the chart. That’s where the legend comes in handy. Basically, it is a relatively small area which describes different parts of the chart.

We will go through three possible options to create a legend in matplotlib.

First Option

Let’s look at an example to make everything clear:

In the upper left corner we have a legend which describes different bars of our chart. To create legend, the plt.legend() function is used with the list of labels as the first argument (this parameter is also called labels).

Second Option

Another option involves specifying the label parameter in each call of the plotting function (bar() in our example):

Here plt.legend() automatically determines the elements to be added to the legend and their labels (all the elements with the label parameter specified are added).

Third Option

In fact, there is even one more option using set_label() method on the artist (bar in our example):

Legend Location

There is another important keyword argument of the legend() function, loc, which specifies the location of the legend. Its default value is best which "tells" the matplotlib to automatically choose the best location for the legend to avoid overlapping with data.

Here we placed the legend in the upper center. Other possible values are the following:

  • 'upper right', 'upper left', 'lower left';
  • 'lower right', 'right';
  • 'center left', 'center right', 'lower center', 'center'.

You can explore more about legend() in the documentation.

Завдання

  1. Label the lowest bars as 'primary sector' specifying the appropriate keyword argument.
  2. Label the bars in the middle as 'secondary sector' specifying the appropriate keyword argument.
  3. Label the top bars as 'tertiary sector' specifying the appropriate keyword argument.
  4. Use the correct function to create a legend.
  5. Place the legend on the right side, centered vertically.

Все було зрозуміло?

Секція 3. Розділ 2
toggle bottom row
course content

Зміст курсу

Ultimate Visualization with Python

Adding LegendAdding Legend

It is often the case when we have multiple elements on the canvas and it would be better to label them in order to describe the chart. That’s where the legend comes in handy. Basically, it is a relatively small area which describes different parts of the chart.

We will go through three possible options to create a legend in matplotlib.

First Option

Let’s look at an example to make everything clear:

In the upper left corner we have a legend which describes different bars of our chart. To create legend, the plt.legend() function is used with the list of labels as the first argument (this parameter is also called labels).

Second Option

Another option involves specifying the label parameter in each call of the plotting function (bar() in our example):

Here plt.legend() automatically determines the elements to be added to the legend and their labels (all the elements with the label parameter specified are added).

Third Option

In fact, there is even one more option using set_label() method on the artist (bar in our example):

Legend Location

There is another important keyword argument of the legend() function, loc, which specifies the location of the legend. Its default value is best which "tells" the matplotlib to automatically choose the best location for the legend to avoid overlapping with data.

Here we placed the legend in the upper center. Other possible values are the following:

  • 'upper right', 'upper left', 'lower left';
  • 'lower right', 'right';
  • 'center left', 'center right', 'lower center', 'center'.

You can explore more about legend() in the documentation.

Завдання

  1. Label the lowest bars as 'primary sector' specifying the appropriate keyword argument.
  2. Label the bars in the middle as 'secondary sector' specifying the appropriate keyword argument.
  3. Label the top bars as 'tertiary sector' specifying the appropriate keyword argument.
  4. Use the correct function to create a legend.
  5. Place the legend on the right side, centered vertically.

Все було зрозуміло?

Секція 3. Розділ 2
toggle bottom row
some-alt