Pyplot:共享轴,子块之间没有空间

2024-04-16 14:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这与new pythonic style for shared axes square subplots in matplotlib?有关(或者更确切地说是后续行动)。

我想让子块共享一个轴,就像上面链接的问题一样。不过,我也不想在情节之间留有空隙。这是我的代码的相关部分:

f, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
plt.setp(ax1, aspect=1.0, adjustable='box-forced')
plt.setp(ax2, aspect=1.0, adjustable='box-forced')

# Plot 1
ax1.matshow(pixels1, interpolation="bicubic", cmap="jet")
ax1.set_xlim((0,500))
ax1.set_ylim((0,500))

# Plot 2
ax2.matshow(pixels2, interpolation="bicubic", cmap="jet")
ax2.set_xlim((0,500))
ax2.set_ylim((0,500))

f.subplots_adjust(wspace=0)

结果是: enter image description here

如果我注释掉这两个plt.setp()命令,就会得到一些添加的白色边框: enter image description here

我怎样才能使这个图形看起来像我的第一个结果,但是在第二个结果中,轴是接触的呢?


Tags: boxtrueplotpltsetinterpolationaspectax1
1条回答
网友
1楼 · 发布于 2024-04-16 14:28:31

编辑:获得结果的最快方法是@Benjamin Bannier描述的方法,只需使用

fig.subplots_adjust(wspace=0)

另一种方法是生成一个宽/高比等于2(因为有两个图)。只有当您计划在文档中包含图形,并且您已经知道最终文档的列宽时,才建议这样做。

您可以在对Figure(figsize=(width,height))的调用中设置宽度和高度,也可以将其作为plt.subplots()的参数(以英寸为单位)。示例:

fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True,figsize=(8,4))
fig.subplots_adjust(0,0,1,1,0,0)

截图:enter image description here

正如@Benjamin Bannier所指出的,作为一个缺点,你的利润率为零。然后你可以玩subplot_adjust(),但是如果你想保持解的简单,你必须小心以对称的方式制造空间。例如fig.subplots_adjust(.1,.1,.9,.9,0,0)

相关问题 更多 >