Contenido del Curso
Ultimate Visualization with Python
Ultimate Visualization with Python
Adding 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:
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()
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):
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()
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.
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()
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.
Tarea
- Label the lowest bars as
'primary sector'
specifying the appropriate keyword argument. - Label the bars in the middle as
'secondary sector'
specifying the appropriate keyword argument. - Label the top bars as
'tertiary sector'
specifying the appropriate keyword argument. - Use the correct function to create a legend.
- Place the legend on the right side, centered vertically.
¡Gracias por tus comentarios!
Adding 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:
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()
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):
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()
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.
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()
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.
Tarea
- Label the lowest bars as
'primary sector'
specifying the appropriate keyword argument. - Label the bars in the middle as
'secondary sector'
specifying the appropriate keyword argument. - Label the top bars as
'tertiary sector'
specifying the appropriate keyword argument. - Use the correct function to create a legend.
- Place the legend on the right side, centered vertically.
¡Gracias por tus comentarios!
Adding 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:
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()
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):
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()
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.
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()
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.
Tarea
- Label the lowest bars as
'primary sector'
specifying the appropriate keyword argument. - Label the bars in the middle as
'secondary sector'
specifying the appropriate keyword argument. - Label the top bars as
'tertiary sector'
specifying the appropriate keyword argument. - Use the correct function to create a legend.
- Place the legend on the right side, centered vertically.
¡Gracias por tus comentarios!
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:
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()
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):
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()
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.
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()
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.
Tarea
- Label the lowest bars as
'primary sector'
specifying the appropriate keyword argument. - Label the bars in the middle as
'secondary sector'
specifying the appropriate keyword argument. - Label the top bars as
'tertiary sector'
specifying the appropriate keyword argument. - Use the correct function to create a legend.
- Place the legend on the right side, centered vertically.