【问题标题】:narrow the gap between subplots in pyplot缩小pyplot中子图之间的差距
【发布时间】:2014-05-03 07:48:52
【问题描述】:

我有四个混淆矩阵要在 pyplot 中绘制。以下是我的代码和结果图片:

confmatmap=cm.YlOrBr    
fig = plt.figure()

plt.clf()
ax = fig.add_subplot(221)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_1), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['T', 'F']); plt.yticks(range(2), ['T', 'F'])

ax = fig.add_subplot(222)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_2), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['','']);plt.yticks(range(2), ['','']);
ax = fig.add_subplot(223)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_3), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['','']);plt.yticks(range(2), ['','']);
ax = fig.add_subplot(224)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf_4![enter image description here][2]), cmap=confmatmap, interpolation='nearest')
plt.xticks(range(2), ['','']);plt.yticks(range(2), ['','']);


fig.subplots_adjust(left=0, bottom=0, right=1, top=1,wspace=0, hspace=0.1)

但是左子图和右子图之间的差距对我来说太大了。我希望垂直和水平间隙的大小大致为一个字符。 我可以通过将hspace 设置为非常小的值来缩小上下子图之间的差距,但wspace 不起作用。 我认为这可能是因为子图有一些空白部分是白色的。

如何缩小此处与任何指定值的差距?

【问题讨论】:

    标签: python matplotlib plot subplot


    【解决方案1】:

    尝试做:

    fig = plt.figure(figsize=(4, 4))
    

    在开始时,您的代码应该可以工作。问题似乎是图形大小与其内容不兼容,因此无法同时尊重wspacehspace

    【讨论】:

      【解决方案2】:

      fig.subplots_adjust(left=0, bottom=0, right=1, top=1) 是正确的方式。

      例如,通过将 left 设置为非零,您可以将左侧的两个子图向内移动,即向右侧的两个子图移动。因此,减小子图之间的宽度。

      这将在您的图形左侧创建额外的空白,因为总大小不会改变。保存图像时,您可以通过包含以下选项来删除它:bbox_inches = "tight"

      您可以使用的另一件事是:

      fig.tight_layout()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 2020-08-05
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多