matplotlib是否有类似suptitle的全局x/y轴标签函数?
suplabel) Great question! Let's break down everything you need to know:
Core Answer
As of modern Matplotlib versions (3.4.0 and later), yes—there are built-in functions for global axis labels, directly analogous to suptitle:
fig.supxlabel()for a global x-axis labelfig.supylabel()for a global y-axis label
These work just like you'd expect: call them on your figure object, and they'll automatically position a single, centered label across all your subplots, no manual coordinate tweaking required.
Example Usage
import matplotlib.pyplot as plt # Create a 2x2 grid of subplots fig, axs = plt.subplots(2, 2) # Plot your data on each subplot here... # Add global axis labels and title fig.supxlabel('Global X-axis Label', fontsize=14) fig.supylabel('Global Y-axis Label', fontsize=14) fig.suptitle('Overall Plot Title', fontsize=16) plt.tight_layout() plt.show()
For Older Matplotlib Versions
If you're stuck on a version before 3.4.0, you can manually add global labels using fig.text() to position text directly on the figure canvas:
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) # Plot your data... # Global x-label (centered at the bottom of the figure) fig.text(0.5, 0.02, 'Global X-axis Label', ha='center', fontsize=12) # Global y-label (centered on the left, rotated vertically) fig.text(0.02, 0.5, 'Global Y-axis Label', va='center', rotation='vertical', fontsize=12) # Adjust layout to prevent label clipping plt.tight_layout(rect=[0.05, 0.05, 0.95, 0.95]) plt.show()
Context on Your GitHub Ticket
You mentioned submitting a ticket about this feature—and that's exactly what drove the official implementation! The functionality you requested is now part of Matplotlib's core, thanks to feedback from users like you pushing for better subplot labeling tools.
内容的提问来源于stack exchange,提问作者Oren

