某些子图之间的间距,但不是全部 [英] Spacing between some subplots but not all

查看:33
本文介绍了某些子图之间的间距,但不是全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中有一个 matplotlib 图,有 3 个子图,全部在 1 列中.

I have a matplotlib plot in python with 3 subplots, all in 1 column.

我目前通过以下方式控制每个子图的高度:

I currently control the height of each subplot with:

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

我没有间距通过:

plt.subplots_adjust(hspace=0.0)

但是我只想在第2行和第3行之间留一些间隔.

But I would like to put some spacing between row 2 and 3 only.

在其他答案之一中,我读到我可以执行以下操作:

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?

推荐答案

当您调用 update 时,您将这些参数应用于该特定 gridspec 中的所有子图.如果要对不同的子图使用不同的参数,则可以制作多个网格规格.但是,您需要确保它们的大小正确且不重叠.一种方法是使用嵌套的 gridspecs.由于底部两个图的总高度是顶部的6倍,因此外部网格规格的高度比为[1,6].

When you call update, you're applying those parameters to all of the subplots in that particular gridspec. If you want to use different parameters for different subplots, you can make multiple 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. 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].

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()

这篇关于某些子图之间的间距,但不是全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆