在python中绘制3张图,2张在顶部,1张在底部轴上



我正在尝试绘制3个树状图,2个在顶部,一个在底部。但我唯一能想到的方法是:

fig, axes = plt.subplots(2, 2, figsize=(22, 14))
dn1 = hc.dendrogram(wardLink, ax=axes[0, 0])
dn2 = hc.dendrogram(singleLink, ax=axes[0, 1])
dn3 = hc.dendrogram(completeLink, ax=axes[1, 0])

在右下角给我第四张空白图。有没有办法只绘制3张图?

您可以根据需要重新划分画布区域,并使用subplot的第三个参数告诉它要打印到哪个单元格:

plt.subplot(2, 2, 1) # divide as 2x2, plot top left
plt.plt(...)
plt.subplot(2, 2, 2) # divide as 2x2, plot top right
plt.plt(...)
plt.subplot(2, 1, 2) # divide as 2x1, plot bottom
plt.plt(...)

您也可以按如下方式使用gridspec

gs = fig.add_gridspec(2, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, :])
...

最新更新