Last Updated : 09 Apr, 2025
Legends helps to understand what each element in the plot represents. They help to understand the meaning behind different elements like colors, markers or line styles. If a plot contains many labels a single-column legend may:
By arranging legend items into multiple columns we can make our plot more compact and visually appealing. matplotlib makes it easy to create multi-column legends and helps us improve the readability and layout of our plots and helps in organizing them more effectively.
Step-by-Step Guide to Create Multi-Column Legends Step 1: Import Required LibrariesTo get started ensure we have Matplotlib installed. If not install it using pip
:
pip install matplotlib
Next import the necessary libraries like numpy and pandas :
Python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Step 2: Create a Plot with Multiple Lines
Let’s begin by creating a simple plot with multiple lines. We create 4 different lines: sine, cosine, tangent and exponential decay.
label=
"argument" gives each line a name for the legend.plt.legend(ncol=2)
splits the legend into 2 columns.loc='upper right'
places the legend in the top-right corner.
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = np.exp(-x)
plt.plot(x, y1, label='Sine')
plt.plot(x, y2, label='Cosine')
plt.plot(x, y3, label='Tangent')
plt.plot(x, y4, label='Exponential')
Output :
Multiple Lines with Multi column -legendThe above plot effectively displays four distinct mathematical functions - Sine, Cosine, Tangent and Exponential on the same graph. The use of a 2-column legend minimizes vertical space and keeps the layout tidy.
Step 3: Customize the Legend AppearanceWe can further customize the legend to suit our needs:
fontsize
parameter.frameon
, edgecolor
and facecolor
.columnspacing
.Example with additional customization:
Python
plt.figure(figsize=(8, 5))
plt.plot(x, y1, label='Sine')
plt.plot(x, y2, label='Cosine')
plt.plot(x, y3, label='Tangent')
plt.plot(x, y4, label='Exponential')
plt.legend(
ncol=3,
loc='upper center',
fontsize=10,
frameon=True,
edgecolor='black',
facecolor='lightgray',
columnspacing=1.5
)
plt.title('Customized Multi-Column Legend')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.tight_layout()
plt.show()
Output:
Customized multi-column legendThis plot visualizes four mathematical functions (sine, tangent, cosine and exponential decay) with a customized multi-column legend placed at the top-center. The legend is styled with distinct colors and labels for each function enhancing readability while the gridlines provide context for the data values.
Step 4: Handle Long Labels and Move Legend Outside PlotIf our legend labels are long, multi-column legends can help prevent them from overlapping. We simulate long labels and shift the sine wave for variety.
bbox_to_anchor=(0.5, -0.25)
positions the legend below the chart and keeps the plot clear and uncluttered.
x = np.linspace(0, 10, 100)
labels = [
'This is a very long label for sine',
'Another long label for cosine',
'Yet another one for tangent',
'And the last one for exponential'
]
plt.figure(figsize=(10, 6))
for i, label in enumerate(labels):
plt.plot(x, np.sin(x + i), label=label)
plt.legend(
ncol=2,
loc='lower center',
bbox_to_anchor=(0.5, -0.25),
fontsize=9,
frameon=True,
columnspacing=1.2
)
plt.title('Handling Long Labels with Multi-Column Legend')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.tight_layout()
plt.show()
Output:
Handling long LabelsThis plot demonstrates four sine waves with slightly different offsets, each labeled with a long descriptive label. The legend is placed at the bottom of the plot in a multi-column layout to accommodate the lengthy labels ensuring clarity and readability while maintaining an organized appearance.
Using multi-column legends in Matplotlib is a simple yet powerful way to enhance the readability. Whether you're working with line plots, bar charts or scatter plots the ncol
parameter allows us to organize our legend efficiently.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4