Python 使用和不使用颜色条对齐matplotlib子批次轴(用于2个以上的轴)

Python 使用和不使用颜色条对齐matplotlib子批次轴(用于2个以上的轴),python,matplotlib,Python,Matplotlib,当一些有颜色条而其他没有时,如何对齐不同子地块的轴 import numpy as np import matplotlib.pyplot as plt data1 = np.random.random([15,15]) data2 = np.random.random(15) fig, [[ax1, ax2], [ax3, ax4], [ax5, ax6]] = plt.subplots(3,2) for ax in [ax1, ax2, ax4, ax5, ax6]: plt.

当一些有颜色条而其他没有时,如何对齐不同子地块的轴

import numpy as np
import matplotlib.pyplot as plt

data1 = np.random.random([15,15])
data2 = np.random.random(15)

fig, [[ax1, ax2], [ax3, ax4], [ax5, ax6]] = plt.subplots(3,2)

for ax in [ax1, ax2, ax4, ax5, ax6]:
    plt.sca(ax)
    plt.pcolormesh(data1)
    plt.colorbar()

plt.sca(ax3)
plt.plot(data2)


我想在
ax3
左侧添加空白,以便与其他图形对齐。

这是迄今为止最好的方法(基于)

我仍然想知道是否有更简单的方法,比如简单地在轴的左侧添加空白

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

data1 = np.random.random([15,15])
data2 = np.random.random(15)


fig, [[ax1, ax2], [ax3, ax4], [ax5, ax6]] = plt.subplots(3,2)

for ax in [ax1, ax2, ax4, ax5, ax6]:
    im1 = ax.pcolormesh(data1, cmap='magma')
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=.05)
    plt.colorbar(im1, cax=cax)

im2 = ax3.plot(data2)
divider2 = make_axes_locatable(ax3)
cax2 = divider2.append_axes("right", size="5%", pad=.05)
cax2.remove()

这是
受约束的\u布局
的设计目的之一:

图,[[ax1,ax2],[ax3,ax4],[ax5,ax6]]=plt.子批(3,2,
受约束(布局=真)

可能与问题中的更好答案重复感谢您为我指出这些答案(不确定为什么它们没有出现在我的搜索中)。然而,不清楚这些答案如何转化为我的具体案例。我更新了我的问题,并询问当图形上有两个以上的轴时如何执行此操作。我猜“简单地在轴的左侧添加空白”将转化为使用链接到的某个帖子。