matplotlib.pyplot.imshow:使用属性"sharex"和"sharey"时删除绘图中的空白区域

mly*_*ynn 12 python whitespace matplotlib

我有一个类似于这里发布的问题.不同之处在于,当我绘制两个通过sharexsharey属性共享轴的子图时,我在绘图区域内得到了不需要的空白区域.设置后,白色空间仍然存在autoscale(False).例如,使用与上述帖子的答案类似的代码:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
ax.imshow(np.random.random((10,10)))
ax.autoscale(False)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax, sharey=ax)   # adding sharex and sharey
ax2.imshow(np.random.random((10,10)))
ax2.autoscale(False)
plt.show()
Run Code Online (Sandbox Code Playgroud)

得到这个图像.

我也曾尝试ax.set_xlim(0, 10)ax.set_xbound(0, 10)按照建议在这里,但无济于事.我怎样才能摆脱多余的空白?任何想法,将不胜感激.

mly*_*ynn 16

如此处所示,添加:

ax.set_adjustable('box-forced')
ax2.set_adjustable('box-forced')
Run Code Online (Sandbox Code Playgroud)

解决了这个问题.

(文件)