
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Show only certain items in legend Python Matplotlib
Using plt.legend(), we can add or show certain items just by putting the values in the list.
Steps
Set the X-axis label using plt.xlabel() method.
Set the Y-axis label using plt.ylabel() method.
Plot the lines using the lists that are passed in the plot() method argument.
Location and legend_drawn flags can help to find a location and make the flag True for 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
- Manually add legend Items Python Matplotlib
- How to show legend elements horizontally in Matplotlib?
- Only show tables with certain patterns in MySQL “show tables”?
- How do you just show the text label in a plot legend in Matplotlib?
- Automated legend creation in Matplotlib
- 3D scatterplots in Python Matplotlib with hue colormap and legend
- Text alignment in a Matplotlib legend
- Legend with vertical line in matplotlib
- Matplotlib histogram with multiple legend entries
- How to show legend and label axes in 3D scatter plots in Python Plotly?
- How to add legend to imshow() in Matplotlib?
- How to create a draggable legend in Matplotlib?
- How to change the legend fontname in Matplotlib?
- Styling a part of label in legend in Matplotlib
- How to check if a string only contains certain characters in Python?

Advertisements