- 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
Legend with vertical line in matplotlib
To add a legend with vertical line in matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a figure and a set of subplots.
- Plot the vertical line with red color.
- The line can have both a solid linestyle connecting all the vertices, and a marker at each vertex.
- Place a legend on the plot with vertical line.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt from matplotlib import lines plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() color = 'red' ax.plot([0, 0], [0, 3], color=color) vertical_line = lines.Line2D([], [], color=color, marker='|', linestyle='None', markersize=10, markeredgewidth=1.5, label='Vertical line') plt.legend(handles=[vertical_line], loc='upper right') plt.show()
Output
It will produce the following output
- Related Articles
- Specifying the line width of the legend frame in Matplotlib
- Matplotlib histogram with multiple legend entries
- Getting vertical gridlines to appear in line plot in matplotlib
- Barchart with vertical labels in Python/Matplotlib
- Matplotlib savefig with a legend outside the plot
- Automated legend creation in Matplotlib
- 3D scatterplots in Python Matplotlib with hue colormap and legend
- Add a legend in a 3D scatterplot with scatter() in Matplotlib
- Text alignment in a Matplotlib legend
- How to align rows in a Matplotlib legend with 2 columns?
- Line plot with arrows in Matplotlib
- How to create a vertical line with CSS?
- Manually add legend Items Python Matplotlib
- Text box with line wrapping in Matplotlib
- How to create a line chart using ggplot2 with a vertical line in R?

Advertisements