

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Drawing multiple legends on the same axes in Matplotlib
To draw multiple legends on the same axes in Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots
- Plot lines using two lists with different labels, linewidth and linestyle.
- Place the first legend at the upper-right location.
- Add artist, i.e., first legend on the current axis.
- Place the second legend on the current axis at the lower-right location.
- To display the figure, use show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 2, 3], label="Line 1", linestyle='--') line2, = plt.plot([3, 2, 1], label="Line 2", linewidth=4) first_legend = plt.legend(handles=[line1], loc='upper right') plt.gca().add_artist(first_legend) plt.legend(handles=[line2], loc='lower right') plt.show()
Output
- Related Questions & Answers
- Multiple axes in Matplotlib with different scales
- Matplotlib legends in subplot
- Drawing multiple figures in parallel in Python with Matplotlib
- What is the difference between drawing plots using plot, axes or figure in matplotlib?
- Creating multiple boxplots on the same graph from a dictionary, using Matplotlib
- How to plot two Pandas time series on the same plot with legends and secondary Y-axis in Matplotlib?
- Drawing circles on an image with Matplotlib and NumPy
- Adding a legend to a Matplotlib boxplot with multiple plots on the same axis
- How to plot multiple histograms on same plot with Seaborn using Matplotlib?
- How to change the space between bars when drawing multiple barplots in Pandas? (Matplotlib)
- How do I plot multiple X or Y axes in Matplotlib?
- How to plot data from multiple two-column text files with legends in Matplotlib?
- How to make simple double head arrows on the axes in Matplotlib?
- How to plot a point on 3D axes in Matplotlib?
- MongoDB multiple OR conditions on same key?
Advertisements