Keywords: Matplotlib | Subplot Layout | Data Visualization | Python Programming | Multi-plot Display
Abstract: This article provides an in-depth exploration of creating multiple independent subplots within a single page or window using the Matplotlib library. Through analysis of common problem scenarios, it thoroughly explains the working principles and parameter configuration of the subplot function, offering complete code examples and best practice recommendations. The content covers everything from basic concepts to advanced usage, helping readers master multi-plot layout techniques for data visualization.
Problem Background and Requirements Analysis
In data visualization projects, there is often a need to display multiple related charts on a single page for comparative analysis or comprehensive presentation. The original code creates multiple independent figure windows through loops, which not only reduces viewing efficiency but may also cause window management chaos. The core issue lies in understanding the conceptual difference between figure windows and subplots in Matplotlib.
Matplotlib Subplot System Analysis
Matplotlib provides a powerful subplot system that allows users to create multiple axis regions within a single figure window. Each subplot is an independent plotting area that can have its own title, labels, and data. The subplot system is based on a grid layout, defined by row and column parameters to establish the overall structure.
Detailed Explanation of subplot Function
The plt.subplot() function is the key tool for solving multi-plot layout problems. This function accepts three main parameters: number of rows, number of columns, and subplot position index. For example, for a layout of 16 subplots, the parameter combination 4, 4, i+1 can be used, where 4 rows and 4 columns form a 16-position grid, and i+1 specifies the current subplot's position within the grid.
Code Refactoring and Implementation
Based on the issues in the original code, we perform the following refactoring:
import csv
import scipy.stats
import numpy
import matplotlib.pyplot as plt
# Create 4x4 subplot layout
for i in range(16):
plt.subplot(4, 4, i+1)
# File selection and data reading
filename = easygui.fileopenbox(msg='Pdf distance 90m contour',
title='select file',
filetypes=['*.csv'],
default='X:\\herring_schools\\')
alt_file = open(filename)
a = []
for row in csv.DictReader(alt_file):
a.append(row['Dist_90m(nmi)'])
y = numpy.array(a, float)
# Probability density calculation
relpdf = scipy.stats.relfreq(y, numbins=7, defaultreallimits=(-10, 60))
bins = numpy.arange(-10, 60, 10)
# Data validation
print(numpy.sum(relpdf[0]))
print(bins)
# Plotting operations
patches = plt.bar(bins, relpdf[0], width=10, facecolor='black')
# Title and label settings
titlename = easygui.enterbox(msg='write graph title',
title='',
default='',
strip=True,
image=None,
root=None)
plt.title(titlename)
plt.ylabel('Probability Density Function')
plt.xlabel('Distance from 90m Contour Line(nm)')
plt.ylim([0, 1])
# Unified display of all subplots
plt.tight_layout()
plt.show()
Parameter Configuration Optimization
In subplot layouts, the call to plt.tight_layout() function is crucial. This function automatically adjusts spacing between subplots, preventing label overlap and ensuring the aesthetics and readability of the overall layout. For complex multi-plot layouts, the plt.subplots_adjust() function can be used for more precise spacing control.
Common Problem Solutions
Regarding the issue of numbins and defaultreallimits parameters not taking effect, this is usually because each subplot is an independent plotting environment. In the refactored code, each subplot recalculates relative frequencies, ensuring consistency in parameter settings. It is recommended to perform parameter validation during the data processing stage to ensure the accuracy of statistical calculations.
Advanced Layout Techniques
For more complex layout requirements, Matplotlib provides the plt.subplots() function, which returns a figure object and an array of subplots, facilitating batch operations. For example:
fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(12, 10))
for i, ax in enumerate(axes.flat):
# Perform plotting operations on each subplot
ax.bar(bins, relpdf_data[i], width=10, facecolor='black')
ax.set_title(f'Graph {i+1}')
ax.set_ylim([0, 1])
Best Practice Recommendations
In practical applications, it is recommended to follow these best practices: reasonably plan the number of subplots to avoid overcrowding; maintain consistent color schemes and font sizes; add detailed annotations to important charts; consider using interactive features to enhance user experience.
Performance Optimization Considerations
For creating large numbers of subplots, attention should be paid to memory management and rendering performance. Performance can be optimized by appropriately adjusting graphic resolution, using vector format output, and processing data in batches. Meanwhile, ensure code maintainability by separating data processing and visualization logic.