python - 如何定位字幕?

标签 python matplotlib

我正在尝试调整 suptitle在多面板图上方,我无法弄清楚如何调整 figsize然后定位字幕。

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

  • suptitle(..., y=1.1) 在哪里?居然放标题?据我所知,y 的文档字幕参数指向matplotlib.text.Text ,但是当您有多个子图时,我不知道图形坐标的含义。
  • 指定 y 时对图形大小有什么影响?至suptitle ?
  • 如何手动调整图形大小和间距(subplots_adjust?)为每个面板添加图形标题和整个图形的副标题,保持图形中每个轴的大小?

  • 一个例子:
    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);
    

    enter image description here

    显然,我每次制作图形时都可以调整 y,但我需要一个通常无需人工干预即可工作的解决方案。我尝试过约束布局和紧凑布局;两者都不能可靠地处理任何复杂的图形。

    最佳答案

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

    图形坐标从 0 到 1,其中 (0,0) 是左下角,(1,1) 是右上角。 y=1.05 的坐标因此稍微超出图形。

    enter image description here

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

    指定 y to suptitle 对图形大小没有任何影响。

    3a。如何手动调整图形大小和间距以添加每个面板的图形标题和整个图形的副标题?

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

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

    enter image description here

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

    只有通过更改整体图形大小,才能保持轴的大小并为标题留出足够的空间。

    如下所示,我们定义了一个函数 make_space_above它将轴数组作为输入,以及新需要的以英寸为单位的上边距。因此,例如,您得出的结论是,您需要在顶部留出 1 英寸的边距来托管您的标题:
    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()
    

    enter image description here

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

    关于python - 如何定位字幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55767312/

    相关文章:

    python - 无法从 'delayed'导入名称 'sklearn.utils.fixes'

    python - 将 Pandas 列拆分为多列

    python - 如何使用 matplotlib.animation 在 python 中对数据图进行动画处理

    python - 如何在 QWidget 中跟踪 matplot Canvas 上的鼠标?

    python:从nums中删除val的两个程序

    python - 制作一个线框多边形

    python - 将 numpy.void 转换为 numpy.ndarray

    python - 无法使用ffmpeg保存matplotlib动画

    python - Matplotlib x 轴日期刻度频率

    python - 如何使用颜色在直方图上表示类别?