Adding Legend
When a chart contains multiple elements, adding a legend helps clarify what each element represents. matplotlib offers several ways to create a legend.
First Option
You can define all labels directly inside plt.legend():
123456789101112131415161718import numpy as np import matplotlib.pyplot as plt questions = ['question_1', 'question_2', 'question_3'] yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) positions = np.arange(len(questions)) 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) plt.legend(['positive answers', 'negative answers']) plt.show()
This creates a legend in the upper-left corner by passing a list of labels into plt.legend().
Second Option
You can also assign labels directly inside plotting functions using the label= parameter:
1234567891011121314151617181920import 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 = [yes_answers, no_answers] labels = ['positive answers', 'negative answers'] width = 0.3 for i in range(len(answers)): plt.bar(positions + width*i, answers[i], width, label=labels[i]) plt.xticks(positions + width*(len(answers)-1)/2, questions) plt.legend() plt.show()
Here, plt.legend() automatically gathers labels from the plotted elements.
Third Option
You can also set labels using the set_label() method of the returned artist:
12345678910111213141516171819202122import 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 = [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]) center_positions = positions + width*(len(answers)-1)/2 plt.xticks(center_positions, questions) plt.legend(loc='upper center') plt.show()
Legend Location
The loc argument controls where the legend appears. The default 'best' asks matplotlib to choose an optimal location automatically.
12345678910111213141516171819202122import 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 = [yes_answers, no_answers] labels = ['positive answers', 'negative answers'] width = 0.3 for i, label in enumerate(labels): bars = plt.bar(positions + width*i, answers[i], width) bars.set_label(label) center_positions = positions + width*(len(answers)-1)/2 plt.xticks(center_positions, questions) plt.legend(loc='upper center') plt.show()
Valid values for loc include:
'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'center'.
You can explore more in legend() documentation
Swipe to start coding
- 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. - Place the legend on the right side, centered vertically.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
Adding Legend
Swipe to show menu
When a chart contains multiple elements, adding a legend helps clarify what each element represents. matplotlib offers several ways to create a legend.
First Option
You can define all labels directly inside plt.legend():
123456789101112131415161718import numpy as np import matplotlib.pyplot as plt questions = ['question_1', 'question_2', 'question_3'] yes_answers = np.array([500, 240, 726]) no_answers = np.array([432, 618, 101]) answers = np.array([yes_answers, no_answers]) positions = np.arange(len(questions)) 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) plt.legend(['positive answers', 'negative answers']) plt.show()
This creates a legend in the upper-left corner by passing a list of labels into plt.legend().
Second Option
You can also assign labels directly inside plotting functions using the label= parameter:
1234567891011121314151617181920import 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 = [yes_answers, no_answers] labels = ['positive answers', 'negative answers'] width = 0.3 for i in range(len(answers)): plt.bar(positions + width*i, answers[i], width, label=labels[i]) plt.xticks(positions + width*(len(answers)-1)/2, questions) plt.legend() plt.show()
Here, plt.legend() automatically gathers labels from the plotted elements.
Third Option
You can also set labels using the set_label() method of the returned artist:
12345678910111213141516171819202122import 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 = [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]) center_positions = positions + width*(len(answers)-1)/2 plt.xticks(center_positions, questions) plt.legend(loc='upper center') plt.show()
Legend Location
The loc argument controls where the legend appears. The default 'best' asks matplotlib to choose an optimal location automatically.
12345678910111213141516171819202122import 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 = [yes_answers, no_answers] labels = ['positive answers', 'negative answers'] width = 0.3 for i, label in enumerate(labels): bars = plt.bar(positions + width*i, answers[i], width) bars.set_label(label) center_positions = positions + width*(len(answers)-1)/2 plt.xticks(center_positions, questions) plt.legend(loc='upper center') plt.show()
Valid values for loc include:
'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'center'.
You can explore more in legend() documentation
Swipe to start coding
- 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. - Place the legend on the right side, centered vertically.
Solution
Thanks for your feedback!
single