A Comprehensive Guide to Creating Multiple Legends on the Same Graph in Matplotlib

Dec 03, 2025 · Programming · 6 views · 7.8

Keywords: Matplotlib | Legend | Data Visualization | Python | Multiple Legends

Abstract: This article provides an in-depth exploration of techniques for creating multiple independent legends on the same graph in Matplotlib. Through analysis of a specific case study—using different colors to represent parameters and different line styles to represent algorithms—it demonstrates how to construct two legends that separately explain the meanings of colors and line styles. The article thoroughly examines the usage of the matplotlib.legend() function, the role of the add_artist() function, and how to manage the layout and display of multiple legends. Complete code examples and best practice recommendations are provided to help readers master this advanced visualization technique.

Introduction and Problem Context

In data visualization, legends are crucial components for explaining the meaning of graphical elements. However, when a graph contains information across multiple dimensions, a single legend may not adequately convey all information. This article discusses a specific technical problem: a user needs to display 12 curves on the same graph, with 4 colors representing different parameters and 3 line styles representing different algorithms. In this scenario, two independent legends are required—one explaining the correspondence between colors and parameters, and another explaining the correspondence between line styles and algorithms.

Fundamentals of the Matplotlib Legend System

Matplotlib's legend system is implemented through the legend() function, which automatically collects information from the label attribute of plot objects. By default, each call to legend() replaces any existing legend. To display multiple legends on the same graph, the add_artist() method must be used to add the first legend to the axes before creating the second legend.

Technical Implementation of Multiple Legends

The following is a complete code example for implementing two legends, reconstructed and expanded based on the best answer from the Q&A:

import matplotlib.pyplot as plt
import itertools

# Initialize colors and parameters
colors = ['b', 'r', 'g', 'c']
parameters = ['param1', 'param2', 'param3', 'param4']
algorithms = ['algo1', 'algo2', 'algo3']

# Create figure and axes
fig, ax = plt.subplots()

# Use itertools.cycle for color cycling
color_cycle = itertools.cycle(colors)
plot_lines = []  # Store plot objects for legends

# Simulate data generation and plotting
for param in parameters:
    c = next(color_cycle)
    
    # Simulate algorithm output data
    d1 = [i + ord(c) for i in range(10)]  # Simulate algo1
    d2 = [i * 2 + ord(c) for i in range(10)]  # Simulate algo2
    d3 = [i ** 1.5 + ord(c) for i in range(10)]  # Simulate algo3
    
    # Plot three curves with different line styles
    line1, = ax.plot(d1, '-', color=c, label=param if len(plot_lines) == 0 else "_nolegend_")
    line2, = ax.plot(d2, '--', color=c)
    line3, = ax.plot(d3, '.-', color=c)
    
    plot_lines.append([line1, line2, line3])

# Create first legend: explain line styles and algorithms
algo_legend = ax.legend(plot_lines[0], algorithms, 
                       title="Algorithms", 
                       loc='upper left',
                       frameon=True)

# Add first legend to axes
ax.add_artist(algo_legend)

# Create second legend: explain colors and parameters
param_lines = [lines[0] for lines in plot_lines]
ax.legend(param_lines, parameters, 
          title="Parameters", 
          loc='lower right',
          frameon=True)

# Set graph properties
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_title('Multiple Algorithms with Different Parameters')
plt.tight_layout()
plt.show()

Code Analysis and Key Technical Points

The core technical aspects of the above code include:

  1. Plot Object Management: By storing plot objects in the plot_lines list, precise control can be maintained over which lines appear in which legend.
  2. Legend Creation Order: First create the algorithm legend and add it to the axes using add_artist(), then create the parameter legend. This is necessary because subsequent calls to legend() would overwrite previous legends, but add_artist() prevents this.
  3. Label Control: When plotting parameter lines, conditional labeling with "_nolegend_" avoids duplicate labels in automatic legends.
  4. Legend Positioning: The loc parameter (e.g., 'upper left' and 'lower right') controls legend placement to prevent overlap.

Advanced Configuration and Customization Options

Matplotlib provides rich customization options for legends:

# Example of custom legend styling
algo_legend = ax.legend(plot_lines[0], algorithms, 
                       title="Algorithms",
                       loc='upper left',
                       frameon=True,
                       shadow=True,
                       fancybox=True,
                       fontsize='small',
                       title_fontsize='medium',
                       borderpad=1.0,
                       labelspacing=0.5)

Key configuration parameters include: frameon controls whether to display a border, shadow adds shadow effects, fancybox creates rounded borders, fontsize and title_fontsize control font sizes, and borderpad and labelspacing adjust internal spacing.

Common Issues and Solutions

Potential problems in practical applications and their solutions:

  1. Legend Overlap: Adjust the loc parameter or use bbox_to_anchor for precise positioning.
  2. Incomplete Legend Content: Ensure the lists of plot objects and labels passed to legend() have consistent lengths.
  3. Performance Issues: When graphs contain numerous curves, consider using Line2D objects to create simplified legends rather than referencing all plot objects.

Best Practice Recommendations

  1. Always clearly define the responsibility scope of each legend to avoid information duplication or omission.
  2. Use descriptive titles (e.g., title="Algorithms") to improve legend readability.
  3. For complex graphs, consider using subplots or faceted plots as alternatives to multiple legends.
  4. Test legend readability across different display environments, particularly when printing or exporting to PDF.

Conclusion

Creating multiple legends in Matplotlib is a powerful visualization technique, especially suitable for complex graphs that need to simultaneously display information across multiple dimensions. Through proper use of the add_artist() method and precise control of plot object references, clear and informative multi-legend graphs can be created. The code examples and technical analysis provided in this article offer reliable references for practical applications, helping users overcome the challenges of multi-dimensional data visualization.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.