Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib.pyplot.imshow:使用属性时删除绘图中的空白;sharex“;及;“分享”;_Python_Matplotlib_Whitespace - Fatal编程技术网

Python matplotlib.pyplot.imshow:使用属性时删除绘图中的空白;sharex“;及;“分享”;

Python matplotlib.pyplot.imshow:使用属性时删除绘图中的空白;sharex“;及;“分享”;,python,matplotlib,whitespace,Python,Matplotlib,Whitespace,我有一个问题,这是一个类似的张贴。不同之处在于,当我通过sharex和sharey属性绘制共享轴的两个子地块时,在绘图区域内会出现不需要的空白。即使设置了自动缩放(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)

我有一个问题,这是一个类似的张贴。不同之处在于,当我通过
sharex
sharey
属性绘制共享轴的两个子地块时,在绘图区域内会出现不需要的空白。即使设置了
自动缩放(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()
结果在图像中


我还按照建议尝试了
ax.set\u xlim(0,10)
ax.set\u xbound(0,10)
,但没有效果。我怎样才能去掉多余的空格?如果您有任何想法,我们将不胜感激。

问题是使用
add\u子批
的有用机制。请注意,如果调整地物的大小,则空白量会发生变化

以下方法似乎有效(直到您重新调整图形的大小)

这看起来像是
对象的大小/位置、共享轴和
imshow
中的相等纵横比之间的不良交互

如果你能在没有蜱虫的情况下生活,你就可以做到

ax.set_axis_off()
ax2.set_axis_off()

我认为值得在matplotlib github上为此打开一个问题。

使用
plt.subplot
作为:

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)
轴内没有空格。 使用
figsize
plt.subplot
fig.subplot\u adjust
中进行调整,可以获得更好的轴比。

如建议,添加:

解决了这个问题


()

在这种情况下,设置
aspect=auto
将消除空白,但这并不总是理想的。我按照你的建议在matplotlib github上开了一期——谢谢。谢谢。但是,我希望找到一种解决方案,使轴的范围不会随着图形大小的改变而改变。@mlynn:你是什么意思?是否希望轴的绝对尺寸始终相同(如5x5 cm)?或者你想让它们保持与图形大小无关的相同纵横比?我想让每个子图显示图像的完整尺寸(在本例中为10x10像素),保持与图形大小无关的相同纵横比。基本上,如果我不在上面的原始代码中添加
sharex
sharey
选项,我就会得到想要的图。我可以调整图形的大小,每个子图都会相应地调整大小。我希望轴共享的原因是,当我放大一个子图时,我希望另一个子图相应地放大。这很有帮助,但如果您不介意丢失纵横比并希望填充所有可用的轴空间,我会添加另一种方法:在
imshow()
中使用
aspect='auto'
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)
ax.set_adjustable('box-forced')
ax2.set_adjustable('box-forced')