首页 > 解决方案 > 条形图通过方面或轴限制子图内或子图之间的约束删除空间

问题描述

我正在努力删除子图中或子图之间的空白。我已经在这里阅读了很多答案,但我没有得到任何地方。我想用几个子图制作水平条形图:

我的例子是:

import matplotlib.pyplot as plt

x1 = [5]
y1 = [-10]

x2 = [30, 35]
y2 = [-15, -20]

x3 = [15, 5, 20]
y3 = [-10, -15, -30]

xlimits = [-30, 35]
ylimits = [-0.5, 2.5]

fig = plt.figure(figsize=(12,6))

ax1 = fig.add_subplot(3,1,1)
ax1.barh(0, x1, height = 1)
ax1.barh(0, y1, height = 1)

ax2 = fig.add_subplot(3,1,2)
ax2.barh([0, 1], x2, height = 1)
ax2.barh([0, 1], y2, height = 1)

ax3 = fig.add_subplot(3,1,3)
ax3.barh([0, 1, 2], x3, height = 1)
ax3.barh([0, 1, 2], y3, height = 1)

for ax in fig.axes:
    ax.set_ylim(ylimits)
    ax.set_xlim(xlimits)

plt.show()

将导致: 带间隙的水平条形图

ax.set_ylim(ylimits)以前所有条的高度都相同,并且ax.set_xlim(xlimits)在一条垂直线上有“0”。

现在我想调整 bbox 以删除子图中的空白区域(顶部和中间)。但我不知道如何实现这一目标。我也试过了ax.set_aspect()。在这种情况下,我将收到子图之间的空白空间。我想用子图来轻松添加描述、交换东西等等。

在此先感谢您的任何建议。

标签: matplotlibbar-chartsubplot

解决方案


如果我理解正确,您可以尝试将其添加到您的代码中:

fig.subplots_adjust(wspace=0, hspace=0)

推荐阅读