- 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 adjust the size of a Matplotlib legend box?
To adjust the size of a matplotlib legend box, we can use borderpad arguments in the legend method.
Steps
Create line1 and line2 using two lists with different line widths.
To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method.
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.35, 1.0), borderpad=2) plt.show()
Output
- Related Articles
- How to Adjust Marker Size in Matplotlib?
- How to set the font size of Matplotlib axis Legend?
- How to adjust the space between legend markers and labels in Matplotlib?
- Adjust the width of box in boxplot in Python Matplotlib
- How to set legend marker size and alpha in Matplotlib?
- How to manually add a legend with a color box on a Matplotlib figure?
- How to increase the font size of the legend in my Seaborn plot using Matplotlib?
- Auto adjust font size in Seaborn heatmap using Matplotlib
- How to adjust the branch lengths of a dendrogram in Matplotlib?
- How to create a draggable legend in Matplotlib?
- How to change the legend fontname in Matplotlib?
- How to add a legend to a Matplotlib pie chart?
- How to move the legend to outside of a Seaborn scatterplot in Matplotlib?
- How to position and align a Matplotlib figure legend?
- How to display the legend of a bar plot in a colored box in R?

Advertisements