Legend with vertical line in matplotlib

To add a legend with a vertical line in matplotlib, you can create a custom legend entry using lines.Line2D. This approach allows you to display a vertical line symbol in the legend while plotting the actual vertical line on the graph.

Steps to Create a Legend with Vertical Line

  • 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 using ax.plot()
  • Create a custom legend entry using lines.Line2D with a vertical marker
  • Add the legend to the plot using plt.legend()
  • Display the figure using plt.show()

Example

Here's how to create a vertical line plot with a custom legend entry ?

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()

# Plot the actual vertical line
color = 'red'
ax.plot([0, 0], [0, 3], color=color)

# Create custom legend entry with vertical line marker
vertical_line = lines.Line2D([], [], color=color, marker='|',
                           linestyle='None', markersize=10,
                           markeredgewidth=1.5, label='Vertical line')

# Add legend with custom handle
plt.legend(handles=[vertical_line], loc='upper right')

plt.show()

The output of the above code is ?

A plot showing a red vertical line from (0,0) to (0,3) with a legend in the upper right corner displaying "Vertical line" with a vertical line symbol.

Key Components Explained

Creating the Custom Legend Entry

The lines.Line2D object creates a custom legend entry with these parameters:

  • [], [] − Empty x and y data (legend symbol only)
  • color='red' − Sets the color of the legend symbol
  • marker='|' − Uses vertical bar as the marker symbol
  • linestyle='None' − No connecting line between markers
  • markersize=10 − Size of the vertical line marker
  • markeredgewidth=1.5 − Width of the marker line
  • label='Vertical line' − Text displayed in the legend

Alternative Markers

You can use different markers to represent vertical elements in legends ?

import matplotlib.pyplot as plt
from matplotlib import lines

fig, ax = plt.subplots()

# Plot multiple vertical lines
ax.plot([1, 1], [0, 3], color='red', linewidth=2)
ax.plot([2, 2], [0, 3], color='blue', linewidth=2)
ax.plot([3, 3], [0, 3], color='green', linewidth=2)

# Create legend entries with different markers
line1 = lines.Line2D([], [], color='red', marker='|', linestyle='None', 
                    markersize=12, label='Type A')
line2 = lines.Line2D([], [], color='blue', marker='_', linestyle='None', 
                    markersize=12, label='Type B')  
line3 = lines.Line2D([], [], color='green', marker='|', linestyle='None', 
                    markersize=12, label='Type C')

plt.legend(handles=[line1, line2, line3], loc='upper right')
plt.xlim(0, 4)
plt.ylim(0, 3)
plt.show()

Conclusion

Using lines.Line2D with custom markers provides full control over legend appearance. The vertical bar marker ('|') effectively represents vertical lines in legends, while linestyle='None' ensures only the marker appears.

---
Updated on: 2026-03-26T13:18:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements