- 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 show legend elements horizontally in Matplotlib?
To show legend elements horizontally, we can take the following steps
- Set the figure size and adjust the padding between and around the subplots.
- Using plot() method, plot lines with the labels line1, line2 and line3.
- Place a legend on the figure using legend() method, with number of labels for ncol value in the argument.
- To display the figure, use show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 2, 3], label="line1") line2, = plt.plot([3, 2, 1], label="line2") line3, = plt.plot([2, 3, 1], label="line3") plt.legend(ncol=3, loc="upper right") plt.show()
Output
- Related Articles
- Show only certain items in legend Python Matplotlib
- How do you just show the text label in a plot legend in Matplotlib?
- How to add legend to imshow() in Matplotlib?
- How to create a draggable legend in Matplotlib?
- How to change the legend fontname in Matplotlib?
- How to add titles to the legend rows in Matplotlib?
- Automated legend creation in Matplotlib
- How to set legend marker size and alpha in Matplotlib?
- Show flex items horizontally in Bootstrap
- How to position and align a Matplotlib figure legend?
- How to center an annotation horizontally over a point in Matplotlib?
- How to show Matplotlib in Flask?
- How to add a legend to a Matplotlib pie chart?
- How to create a legend for a 3D bar in Matplotlib?
- How to align rows in a Matplotlib legend with 2 columns?

Advertisements