简体   繁体   English

一些子图之间的间距,但不是全部

[英]Spacing between some subplots but not all

I have a matplotlib plot in python with 3 subplots, all in 1 column.我在 python 中有一个 matplotlib 图,有 3 个子图,全部在 1 列中。

I currently control the height of each subplot with:我目前控制每个子图的高度:

gridspec.GridSpec(3, 1, height_ratios=[1, 3, 3])

I have no spacing via:我没有间距通过:

plt.subplots_adjust(hspace=0.0)

But I would like to put some spacing between row 2 and 3 only.但我只想在第 2 行和第 3 行之间放置一些间距。

In one of the other answers, I read that I can do something like:在其他答案之一中,我读到我可以执行以下操作:

gs1.update(left=0.05, right=0.48, wspace=0)

But I don't really understand what is happening.但我真的不明白发生了什么。 Could someone give me some more information please?有人可以给我更多的信息吗?

When you call update, you're applying those parameters to all of the subplots in that particular gridspec.当您调用 update 时,您将这些参数应用于该特定 gridspec 中的所有子图。 If you want to use different parameters for different subplots, you can make multiple gridspecs.如果要对不同的子图使用不同的参数,可以制作多个 gridspecs。 However, you'll need to make sure they are the correct size and don't overlap.但是,您需要确保它们的大小正确且不重叠。 One way do to that is with nested gridspecs.一种方法是使用嵌套的 gridspecs。 Since the total height of the bottom two plots is 6 times the top, the outer gridspec will have a height ratio of [1, 6].由于底部两个图的总高度是顶部的 6 倍,因此外部 gridspec 的高度比为 [1, 6]。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


def do_stuff(cell): #just so the plots show up
    ax = plt.subplot(cell)
    ax.plot()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.subplots_adjust(hspace=0.0)
#make outer gridspec
outer = gridspec.GridSpec(2, 1, height_ratios = [1, 6]) 
#make nested gridspecs
gs1 = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec = outer[0])
gs2 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec = outer[1], hspace = .05)
for cell in gs1:
    do_stuff(cell)
for cell in gs2:
    do_stuff(cell)
plt.show()

用三个子图绘制

In this particular case it's probably quickest to just add an invisible axes object between rows 2 and 3:在这种特殊情况下,在第 2 行和第 3 行之间添加一个不可见的轴对象可能是最快的:

import matplotlib.pyplot as plt

gridspec = dict(hspace=0.0, height_ratios=[1, 1, 0.4, 3])
fig, axs = plt.subplots(nrows=4, ncols=1, gridspec_kw=gridspec)
axs[2].set_visible(False)

I looked through the documentation and it appears that variable grid spacing is not supported.我查看了文档,似乎不支持可变网格间距。 So we have to make do with workarounds like this one.所以我们必须设法解决这样的问题。

子图

You can use gridspec and define an extra subplot without content and give it a small width ratio depending on the space you want to exist between the other subplots.您可以使用 gridspec 并定义一个没有内容的额外子图,并根据您希望在其他子图之间存在的空间给它一个小的宽度比。

spec = gridspec.GridSpec(ncols=4, nrows=1, figure=fig,width_ratios=[1,1,0.4,2])
ax00  = fig.add_subplot(spec[0, 0])
ax01  = fig.add_subplot(spec[0, 1])
ax03  = fig.add_subplot(spec[0, 3])

In my example 02 subplot is used only for spacing with a width ratio of 0.4 :).在我的示例中,02 子图仅用于宽度比为 0.4 的间距:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM