gpt4 book ai didi

python - 绘图 : Shared axes and no space between subplots

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

这与 new pythonic style for shared axes square subplots in matplotlib? 有关(或者更确切地说是跟进) .

我想让子图共享一个轴,就像上面链接的问题一样。但是,我也不希望地 block 之间没有空间。这是我的代码的相关部分:

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

如何使图形看起来像我的第一个结果,但轴像第二个结果那样接触?

最佳答案

编辑:获得结果的最快方法是@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)

关于python - 绘图 : Shared axes and no space between subplots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607444/

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