如何定位字幕? [英] How to position suptitle?

查看:37
本文介绍了如何定位字幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在多面板图形上方调整字幕,但在弄清楚如何调整 figsize 并随后放置字幕时遇到了麻烦.

问题是调用 plt.suptitle("my title", y=...) 调整 suptitle 的位置也会调整图形尺寸.几个问题:

  1. suptitle(...,y = 1.1)实际放置标题在哪里?据我所知,字幕的 y 参数的文档指向

    很显然,每次我制作一个数字时,我都可以进行调整,但是我需要一种通常无需人工干预即可工作的解决方案.我已经尝试过受限布局和紧凑布局.两者都不能可靠地处理任何复杂的图形.

    解决方案

    1.图形坐标是什么意思?

    图形坐标从 0 到 1,其中 (0,0) 是左下角,(1,1) 是右上角.因此, y = 1.05 的坐标略微位于图形之外.

    2.指定 y 为 suptitle 时对图形大小有何影响?

    y 指定为 suptitle 对图形大小没有任何影响.

    3a.如何手动调整图形尺寸和间距以在每个面板上添加图形标题并为整个图形添加字幕?

    首先,不会添加其他换行符.IE.如果要有2行,请不要使用3个换行符( \ n ).然后可以根据需要调整子图参数,为标题留出空间.例如. fig.subplots_adjust(top = 0.8),并使用 y< = 1 将该标题放在图形中.

     将matplotlib.pyplot导入为plt将numpy导入为np数据 = np.random.random(大小=100)图,轴 = plt.subplots(2, 2, figsize=(10, 5))fig.subplots_adjust(top=0.8)axes [0,0] .plot(数据)axis[0,0].set_title("\n".join(["这是一个很长的标题"]*2))轴[0,1].plot(数据)轴 [1,1].plot(数据)fig.suptitle("\n".join(["一个大长的suptitle碰到标题"]*2), y=0.98)plt.show()

    3b....同时保持图中每个斧头的大小?

    只有通过更改整体图形大小才能保持轴的大小并仍然有足够的标题空间.

    看起来如下所示,在这里我们定义了一个函数 make_space_above ,该函数将轴数组作为输入,以及新希望的以英寸为单位的上边距.因此,例如,您得出的结论是,您需要在顶部留出1英寸的空白以容纳标题:

     将matplotlib.pyplot导入为plt将numpy导入为np数据 = np.random.random(大小=100)图,轴 = plt.subplots(2, 2, figsize=(10, 5),squeeze = False)轴[0,0].plot(数据)axes [0,0] .set_title("\ n" .join([[这是一个很长的标题] * 2))axes [0,1] .plot(数据)轴 [1,1].plot(数据)fig.suptitle("\n".join(["一个大长的suptitle碰到标题"]*2), y=0.98)def make_space_above(axes, topmargin=1):"增加图形大小以为标题,不改变轴大小"""无花果= axes.flatten()[0].图s =图子图w,h = fig.get_size_inches()figh = h-(1-s.top)* h + topmarginfig.subplots_adjust(bottom=s.bottom*h/figh, top=1-topmargin/figh)fig.set_figheight(figh)make_space_above(轴,topmargin = 1)plt.show()

    (左:未调用 make_space_above ;右:已调用 make_space_above(axes,topmargin = 1))

    I'm trying to adjust a suptitle above a multi-panel figure and am having trouble figuring out how to adjust the figsize and subsequently position the suptitle.

    The problem is that calling plt.suptitle("my title", y=...) to adjust the position of the suptitle also adjusts the figure dimensions. A few questions:

    1. where does suptitle(..., y=1.1) actually put the title? As far as I can tell, the documentation for the y parameter of suptitle points to matplotlib.text.Text, but I don't know what figure coordinates mean when you have multiple subplots.

    2. what is the effect on figure size when specifying y to suptitle?

    3. how do I manually adjust figure size and spacing (subplots_adjust?) to add a figure title per panel and a suptitle for the entire figure, maintaining the size of each ax in the figure?

    An example:

    data = np.random.random(size=100)
    f, a = plt.subplots(2, 2, figsize=(10, 5))
    
    a[0,0].plot(data)
    a[0,0].set_title("this is a really long title\n"*2)
    a[0,1].plot(data)
    a[1,1].plot(data)
    
    plt.suptitle("a big long suptitle that runs into the title\n"*2, y=1.05);
    

    Obviously I can tweak y each time I make a figure, but I need a solution that generally works without manual intervention. I've tried both constrained layout and tight layout; neither works reliably with figures of any complexity.

    解决方案

    1. What do figure coordinates mean?

    Figure coordinates go 0 to 1, where (0,0) is the lower left corner and (1,1) is the upper right corner. A coordinate of y=1.05 is hence slightly outside the figure.

    2. what is the effect on figure size when specifying y to suptitle?

    Specifying y to suptitle has no effect whatsoever on the figure size.

    3a. How do I manually adjust figure size and spacing to add a figure title per panel and a suptitle for the entire figure?

    First, one would not add an additional linebreak. I.e. if you want to have 2 lines, don't use 3 linebreaks (\n). Then one can adjust the subplot parameters as desired to leave space for the titles. E.g. fig.subplots_adjust(top=0.8) and use a y <= 1 for the title to be inside the figure.

    import matplotlib.pyplot as plt
    import numpy as np
    
    data = np.random.random(size=100)
    fig, axes = plt.subplots(2, 2, figsize=(10, 5))
    fig.subplots_adjust(top=0.8)
    
    axes[0,0].plot(data)
    axes[0,0].set_title("\n".join(["this is a really long title"]*2))
    axes[0,1].plot(data)
    axes[1,1].plot(data)
    
    fig.suptitle("\n".join(["a big long suptitle that runs into the title"]*2), y=0.98)
    
    plt.show()
    

    3b. ... while maintaining the size of each ax in the figure?

    Maintaining the size of the axes and still have enough space for the titles is only possible by changing the overall figure size.

    This could look as follows, where we define a function make_space_above which takes the array of axes as input, as well as the newly desired top margin in units of inches. So for example, you come to the conclusion that you need 1 inch of margin on top to host your titles:

    import matplotlib.pyplot as plt
    import numpy as np
    
    data = np.random.random(size=100)
    fig, axes = plt.subplots(2, 2, figsize=(10, 5), squeeze = False)
    
    axes[0,0].plot(data)
    axes[0,0].set_title("\n".join(["this is a really long title"]*2))
    axes[0,1].plot(data)
    axes[1,1].plot(data)
    
    fig.suptitle("\n".join(["a big long suptitle that runs into the title"]*2), y=0.98)
    
    
    def make_space_above(axes, topmargin=1):
        """ increase figure size to make topmargin (in inches) space for 
            titles, without changing the axes sizes"""
        fig = axes.flatten()[0].figure
        s = fig.subplotpars
        w, h = fig.get_size_inches()
    
        figh = h - (1-s.top)*h  + topmargin
        fig.subplots_adjust(bottom=s.bottom*h/figh, top=1-topmargin/figh)
        fig.set_figheight(figh)
    
    
    make_space_above(axes, topmargin=1)    
    
    plt.show()
    

    (left: without calling make_space_above; right: with call to make_space_above(axes, topmargin=1))

    这篇关于如何定位字幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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