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
• 27,160 points
466 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
• 58,500 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
80 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,780 points
2,680 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,640 points
219 views
+1 vote
8 answers

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

Just for the record: >>> int('55063.000000') Traceback (most recent ...READ MORE

answered Aug 17 in Python by pakainfo
• 200 points
148,251 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 1,357 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
• 58,500 points
71 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
• 58,500 points
104 views