- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Manually add legend Items Python Matplotlib
Using plt.legend() method, we can create a legend, and passing frameon would help to keep the border over there.
Steps
Set the X-axis label using plt.xlabel() method.
Set the Y-axis label using plt.ylabel() method.
Draw lines using plot() method.
Location and legend drawn flags can help to find a location and make the flag True for the border.
Set the legend with “blue” and “orange” elements.
To show the figure use plt.show() method.
Example
import matplotlib.pyplot as plt plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.plot([9, 5], [2, 5], [4, 7, 8]) location = 0 # For the best location legend_drawn_flag = True plt.legend(["blue", "orange"], loc=0, frameon=legend_drawn_flag) plt.show()
Output
- Related Articles
- Show only certain items in legend Python Matplotlib
- How to add legend to imshow() in Matplotlib?
- How to add a legend to a Matplotlib pie chart?
- How to add titles to the legend rows in Matplotlib?
- Add a legend in a 3D scatterplot with scatter() in Matplotlib
- How to add a legend on Seaborn facetgrid bar plot using Matplotlib?
- Automated legend creation in Matplotlib
- 3D scatterplots in Python Matplotlib with hue colormap and legend
- Legend with vertical line in matplotlib
- Matplotlib histogram with multiple legend entries
- Text alignment in a Matplotlib legend
- How to show legend elements horizontally in Matplotlib?
- How to change the legend fontname in Matplotlib?
- How to create a draggable legend in Matplotlib?
- Matplotlib savefig with a legend outside the plot

Advertisements