简体   繁体   English

matplotlib tiny_layout + gridspec + fig.suptitle 看起来很糟糕

[英]matplotlib tight_layout + gridspec + fig.suptitle looks bad

I have a very nice GridSpec graph using Matplotlib 2.2.2 but I can't make a pretty title for the figure as a whole.我有一个使用 Matplotlib 2.2.2 的非常漂亮的 GridSpec 图,但我无法为整个图制作一个漂亮的标题。 Example:例子:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
%matplotlib inline

def example(tl):
    fig = plt.figure(figsize=(14,8))
    hr = [3,3,3,1,1]
    wr = [3,1]
    ny = len(hr)
    nx = len(wr)
    gs = gridspec.GridSpec(ny,nx,
                        height_ratios=hr,
                        width_ratios=wr,
                        hspace=0.08, wspace=0.1)
    for j in xrange(nx):
        ax = [fig.add_subplot(gs[0,j])]
        ax += [fig.add_subplot(gs[i,j], sharex=ax[0]) for i in xrange(1,ny)]
        for axi in ax:
              axi.plot([0,1,2],[0,1,4])    
    fig.suptitle('The quick brown fox jumps over the lazy dog.')
    if tl:
        gs.tight_layout(fig)

If I run example(False) for no tight layout I get a huge amount of space above the figures:如果我运行example(False)没有紧凑的布局,我会在数字上方获得大量空间:

在此处输入图片说明

If I run example(True) for a tight layout I get negative space:如果我运行example(True)以获得紧凑的布局,我会得到负空间:

在此处输入图片说明

How can I fix this and get a figure-level title with a proper amount of margin from the subplots?如何解决此问题并从子图中获得具有适当边距的图形级标题?

tight_layout() does not take figure level artists into account. tight_layout()不考虑图形级别的艺术家。

Use constrained_layout使用 constrained_layout

However, there is a relatively new alternative, called constrained_layout .但是,有一个相对较新的替代方案,称为constrained_layout Using this, the figure title will be included.使用这个,图形标题将被包括在内。 Note that for this to work you need to supply the figure to the GridSpec via it's figure argument.请注意,要使其正常工作,您需要通过其figure参数将图形提供给GridSpec

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

def example(tl):
    fig = plt.figure(figsize=(14,8), constrained_layout=tl)
    hr = [3,3,3,1,1]
    wr = [3,1]
    ny = len(hr)
    nx = len(wr)
    gs = gridspec.GridSpec(ny,nx, figure=fig,
                        height_ratios=hr,
                        width_ratios=wr,
                        hspace=0.08, wspace=0.1)
    for j in range(nx):
        ax = [fig.add_subplot(gs[0,j])]
        ax += [fig.add_subplot(gs[i,j], sharex=ax[0]) for i in range(1,ny)]
        for axi in ax:
              axi.plot([0,1,2],[0,1,4])    
    fig.suptitle('The quick brown fox jumps over the lazy dog.')


example(True)
plt.show()

在此处输入图片说明

Update top margin更新上边距

Alternatively you can update the top margin after calling tight_layout .或者,您可以调用tight_layout更新上边距。 Eg as例如作为

gs.update(top=0.95)

Code:代码:

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

def example(tl):
    fig = plt.figure(figsize=(14,8))
    hr = [3,3,3,1,1]
    wr = [3,1]
    ny = len(hr)
    nx = len(wr)
    gs = gridspec.GridSpec(ny,nx, figure=fig,
                        height_ratios=hr,
                        width_ratios=wr,
                        hspace=0.08, wspace=0.1)
    for j in range(nx):
        ax = [fig.add_subplot(gs[0,j])]
        ax += [fig.add_subplot(gs[i,j], sharex=ax[0]) for i in range(1,ny)]
        for axi in ax:
              axi.plot([0,1,2],[0,1,4])    
    fig.suptitle('The quick brown fox jumps over the lazy dog.')
    if tl:
        gs.tight_layout(fig)
        gs.update(top=0.95)


example(True)
plt.show()

在此处输入图片说明

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

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