gpt4 book ai didi

python - matplotlib.pyplot.imshow : removing white space within plots when using attributes "sharex" and "sharey"

转载 作者:太空狗 更新时间:2023-10-29 17:42:26 25 4
gpt4 key购买 nike

我遇到了一个类似于 here 上发布的问题.不同之处在于,当我绘制两个通过 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()

结果为 this图片。

我还根据建议尝试了 ax.set_xlim(0, 10)ax.set_xbound(0, 10) here ,但无济于事。我怎样才能摆脱多余的空白?任何想法将不胜感激。

最佳答案

按照建议here ,添加:

ax.set_adjustable('box-forced')
ax2.set_adjustable('box-forced')

解决问题。

( documentation )

关于python - matplotlib.pyplot.imshow : removing white space within plots when using attributes "sharex" and "sharey",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com