- 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 do you just show the text label in a plot legend in Matplotlib?
To just show the text label in plot legend we can use legend method with handlelength=0, handletextpad=0 and fancybox=0 in the arguments.
Steps
Set the figure size and adjust the padding between and around the subplots.
Create random x and y data points using numpy.
Create a figure and a set of subplots using subplots() method.
Plot x and y data points using plot() method with label "Zig-Zag" for the legend.
Use legend() method to place the label for the plot with handlelength=0, handletextpad=0 and fancybox=0 in the arguments.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(100) y = np.random.rand(100) fig, ax = plt.subplots() ax.plot(x, y, label='Zig-Zag', c="red") ax.legend(loc="upper right", handlelength=0, handletextpad=0, fancybox=True) plt.show()
Output
- Related Articles
- How do you create a legend for a contour plot in Matplotlib?
- Text alignment in a Matplotlib legend
- Styling a part of label in legend in Matplotlib
- How to show legend elements horizontally in Matplotlib?
- Matplotlib savefig with a legend outside the plot
- How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file?
- How to make two markers share the same label in the legend using Matplotlib?
- Plot animated text on the plot in Matplotlib
- Show only certain items in legend Python Matplotlib
- How to add text inside a plot in Matplotlib?
- How to show legend and label axes in 3D scatter plots in Python Plotly?
- Rotating axes label text in 3D Matplotlib
- How to curve text in a polar plot in matplotlib?
- How to change the text color of font in the legend using Matplotlib?
- How to place customized legend symbols on a plot using Matplotlib?

Advertisements