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

使用Matplotlib make_axes_locatable时轴标签未对齐问题求助

解决make_axes_locatable布局下轴标签未对齐的问题

问题背景

使用make_axes_locatable创建带直方图和色条的布局时,轴标签无法通过fig.align_labels()对齐,Matplotlib版本3.6.0。

原因

make_axes_locatable创建的附加轴(如顶部直方图、右侧直方图)属于AxesDivider管理的嵌套轴,fig.align_labels()默认不会自动处理这类轴的标签对齐;再加上轴设置了sharex/sharey或固定宽高比,进一步干扰了标签的位置计算逻辑。

解决方案

方案1:手动调整标签位置

先绘制一次获取主坐标轴标签的位置,再将其他轴的标签同步到该位置,确保对齐:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, ax = plt.subplots(constrained_layout=True)  # 改用constrained_layout替代tight_layout
ax.set_aspect("equal")
divider = make_axes_locatable(ax)

ax_histx = divider.append_axes("top", size="25%", pad="0.75%", sharex=ax)
ax_histy = divider.append_axes("right", size="25%", pad="0.75%", sharey=ax)
cax = divider.append_axes("right", size="4%", pad="0.75%")
hcax = divider.append_axes("top", size="4%", pad="0.75%")

ax.set_xlim(0,10000)
ax.set_ylim(0,10000)
ax.tick_params(axis='x', rotation=90)

# 设置标签
ax.set_xlabel("xlabel")
ax_histy.set_xlabel("xlabel")
ax.set_ylabel("ylabel")
ax_histx.set_ylabel("ylabel")

# 先绘制一次获取标签位置
fig.canvas.draw()
# 对齐x轴标签
x_label_pos = ax.xaxis.get_label().get_position()
ax_histy.xaxis.get_label().set_position(x_label_pos)
# 对齐y轴标签
y_label_pos = ax.yaxis.get_label().get_position()
ax_histx.yaxis.get_label().set_position(y_label_pos)

plt.show()

方案2:改用GridSpec布局替代make_axes_locatable

用matplotlib.gridspec.GridSpec更精准地控制轴的位置,让标签自然对齐:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(constrained_layout=True)
# 定义4行4列网格,分配各轴位置
gs = gridspec.GridSpec(4, 4, figure=fig)

ax = fig.add_subplot(gs[1:4, 0:3])
ax_histx = fig.add_subplot(gs[0, 0:3], sharex=ax)
ax_histy = fig.add_subplot(gs[1:4, 3], sharey=ax)
cax = fig.add_subplot(gs[1:4, 4])  # 可调整列数适配色条
hcax = fig.add_subplot(gs[0, 3])

ax.set_aspect("equal")
ax.set_xlim(0,10000)
ax.set_ylim(0,10000)
ax.tick_params(axis='x', rotation=90)

ax.set_xlabel("xlabel")
ax_histy.set_xlabel("xlabel")
ax.set_ylabel("ylabel")
ax_histx.set_ylabel("ylabel")

fig.align_labels()  # 此时align_labels可正常工作

plt.show()

方案3:禁用轴共享并强制对齐标签

如果不需要轴共享,去掉sharex/sharey后,直接指定要对齐的轴:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, ax = plt.subplots(constrained_layout=True)
ax.set_aspect("equal")
divider = make_axes_locatable(ax)

# 去掉sharex/sharey,手动同步轴范围
ax_histx = divider.append_axes("top", size="25%", pad="0.75%")
ax_histy = divider.append_axes("right", size="25%", pad="0.75%")
cax = divider.append_axes("right", size="4%", pad="0.75%")
hcax = divider.append_axes("top", size="4%", pad="0.75%")

ax.set_xlim(0,10000)
ax.set_ylim(0,10000)
ax_histx.set_xlim(0,10000)
ax_histy.set_ylim(0,10000)
ax.tick_params(axis='x', rotation=90)

ax.set_xlabel("xlabel")
ax_histy.set_xlabel("xlabel")
ax.set_ylabel("ylabel")
ax_histx.set_ylabel("ylabel")

# 强制对齐指定轴的标签
fig.align_xlabels([ax, ax_histy])
fig.align_ylabels([ax, ax_histx])

plt.show()

内容的提问来源于stack exchange,提问作者RaphBilder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 19:57:26