- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to position and align a Matplotlib figure legend?
To position and align a matplotlib figure legend, we can take the following steps−
- Plot line1 and line2 using plot() method.
- Place a legend on the figure. Use bbox_to_anchor to set the position and make horizontal alignment of the legend elements.
- To display the figure, use show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 5, 1, 7], linewidth=0.7) line2, = plt.plot([5, 1, 7, 1], linewidth=2.0) plt.legend([line1, line2], ["line1", "line2"], bbox_to_anchor=(0.45, 1.0), ncol=2) plt.show()
Output
- Related Articles
- How to align rows in a Matplotlib legend with 2 columns?
- How to create a draggable legend in Matplotlib?
- How to add a legend to a Matplotlib pie chart?
- How to set legend marker size and alpha in Matplotlib?
- How to add legend to imshow() in Matplotlib?
- How to adjust the size of a Matplotlib legend box?
- How to show legend elements horizontally in Matplotlib?
- How to change the legend fontname in Matplotlib?
- How to create a legend for a 3D bar in Matplotlib?
- Python Plotly – How to hide legend entries in a Plotly figure?
- Text alignment in a Matplotlib legend
- How to modify a Matplotlib legend after it has been created?
- How to place customized legend symbols on a plot using Matplotlib?
- How to adjust the space between legend markers and labels in Matplotlib?
- How to add titles to the legend rows in Matplotlib?

Advertisements