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

Course Content

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

bookAdding 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:

1234567891011121314
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Setting the labels for the legend explicitly plt.legend(['positive answers', 'negative answers']) plt.show()
copy

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):

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): # Specifying the label parameter for each of the bar() function calls plt.bar(positions + width * i, answers[i], width, label=labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Automatical detection of the labels plt.legend() plt.show()
copy

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.

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): bar = plt.bar(positions + width * i, answers[i], width) bar.set_label(labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Modiying the legend location plt.legend(loc='upper center') plt.show()
copy

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.

Task

  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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookAdding 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:

1234567891011121314
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Setting the labels for the legend explicitly plt.legend(['positive answers', 'negative answers']) plt.show()
copy

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):

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): # Specifying the label parameter for each of the bar() function calls plt.bar(positions + width * i, answers[i], width, label=labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Automatical detection of the labels plt.legend() plt.show()
copy

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.

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): bar = plt.bar(positions + width * i, answers[i], width) bar.set_label(labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Modiying the legend location plt.legend(loc='upper center') plt.show()
copy

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.

Task

  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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookAdding 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:

1234567891011121314
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Setting the labels for the legend explicitly plt.legend(['positive answers', 'negative answers']) plt.show()
copy

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):

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): # Specifying the label parameter for each of the bar() function calls plt.bar(positions + width * i, answers[i], width, label=labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Automatical detection of the labels plt.legend() plt.show()
copy

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.

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): bar = plt.bar(positions + width * i, answers[i], width) bar.set_label(labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Modiying the legend location plt.legend(loc='upper center') plt.show()
copy

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.

Task

  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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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:

1234567891011121314
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 for i in range(len(answers)): plt.bar(positions + width * i, answers[i], width) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Setting the labels for the legend explicitly plt.legend(['positive answers', 'negative answers']) plt.show()
copy

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):

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): # Specifying the label parameter for each of the bar() function calls plt.bar(positions + width * i, answers[i], width, label=labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Automatical detection of the labels plt.legend() plt.show()
copy

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.

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np questions = ['question_1', 'question_2', 'question_3'] positions = np.arange(len(questions)) yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) width = 0.3 labels = ['positive answers', 'negative answers'] for i in range(len(answers)): bar = plt.bar(positions + width * i, answers[i], width) bar.set_label(labels[i]) plt.xticks(positions + width * (len(answers) - 1) / 2, questions) # Modiying the legend location plt.legend(loc='upper center') plt.show()
copy

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.

Task

  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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt