You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

matplotlib是否有类似suptitle的全局x/y轴标签函数?

Global X/Y Axis Labels in Matplotlib (Like Matlab's 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 label
  • fig.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:44:26