我有一个类似于在here上发布的问题。不同之处在于,当我通过sharex和sharey属性绘制两个共享轴的子图时,我会在绘图区域中看到不需要的空格。即使在设置了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()生成this图像。
我也尝试过ax.set_xlim(0, 10)和ax.set_xbound(0, 10),根据here的建议,但都没有用。我怎样才能去掉多余的空格?任何想法都将不胜感激。
发布于 2013-02-28 06:38:49
发布于 2013-02-27 04:18:07
将plt.subplots用作:
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=False)
ax[0].imshow(np.random.random((10,10)))
ax[0].autoscale(False)
ax[1].imshow(np.random.random((10,10)))
ax[1].autoscale(False)我得到了

轴中没有空格。在plt.subplots或fig.subplots_adjust中使用figsize可以获得更好的轴比。
发布于 2013-02-26 06:59:18
问题在于使用add_subplot的有用机制。请注意,如果调整插图的大小,则空白的数量会发生变化。
下面的方法似乎有效(直到您重新调整了图形的大小)
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([.3, .55, .35, .35])
ax.imshow(np.random.random((10,10)))
ax.autoscale(False)
ax2 = fig.add_axes([.3, .05, .35, .35], sharex=ax, sharey=ax )
ax2.imshow(np.random.random((10,10)))
ax2.autoscale(False)
plt.show()这看起来像是axes对象的大小/位置、共享轴和来自imshow的相等纵横比之间的不良交互。
如果你能活着没有扁虫,你可以做到
ax.set_axis_off()
ax2.set_axis_off()我认为值得在matplotlib github上为此打开一个问题。
https://stackoverflow.com/questions/15077364
复制相似问题