How to make a single legend for many subplots with matplotlib?

0 votes
I am plotting the same type of information, but for different countries, with multiple subplots with matplotlib. That is, I have 9 plots on a 3x3 grid, all with the same for lines (of course, different values per line).

However, I have not figured out how to put a single legend (since all 9 subplots have the same lines) on the figure just once.

How do I do that?
May 7 in Python by kartik
• 11,890 points
105 views

1 answer to this question.

0 votes

Hello @kartik,

There is  nice function get_legend_handles_labels() you can call on the last axis (if you iterate over them) that would collect everything you need from label= arguments:

handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')

Thank You!

answered May 7 by Niroj
• 23,950 points

Related Questions In Python

0 votes
1 answer

Is it possible to create a dictionary with multiple keys for a single value?

One thing that you can do here ...READ MORE

answered Jul 24, 2019 in Python by Arvind
• 2,980 points
58 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,760 points
2,344 views
0 votes
1 answer

In python how to test a string for a substring

if "ABCD" in "xxxxABCDyyyy": This can be used ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 57,530 points
191 views
+1 vote
3 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

The error message invalid literal for int() ...READ MORE

answered Apr 29 in Python by warrenfelsh
• 140 points
78,775 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 963 views
0 votes
0 answers
0 votes
1 answer

How to make a flat list out of list of lists?

Hii @kartik, Given a list of lists l, flat_list = ...READ MORE

answered Apr 13 in Python by Niroj
• 23,950 points
29 views
0 votes
1 answer

How to make a chain of function decorators?

Hello @kartik, Python decorators add extra functionality to ...READ MORE

answered Apr 13 in Python by Niroj
• 23,950 points
22 views