如何定位副标题?

2024-05-23 17:55:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图调整一个多面板图形上方的suptitle,但在计算如何调整figsize并随后定位副标题时遇到问题。

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

  1. suptitle(..., y=1.1)实际上把标题放在哪里?据我所知,suptitle的y参数的文档指向matplotlib.text.Text,但我不知道当有多个子块时,图形坐标是什么意思。

  2. 当指定ysuptitle时,对图形大小有什么影响?

  3. 如何手动调整图形大小和间距(subplots_adjust?)为每个面板添加一个图形标题,为整个图形添加一个副标题,保持图形中每个ax的大小?

例如:

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,但是我需要一个不需要人工干预的解决方案。我试过约束布局和紧凑布局;两者都不能可靠地处理任何复杂的图形。


Tags: 图形面板标题dataplottitlepltrandom
2条回答

1。数字坐标是什么意思?

图形坐标从0到1,其中(0,0)是左下角,(1,1)是右上角。因此,y=1.05的坐标略在图外。

enter image description here

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

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

3a.如何手动调整图形大小和间距,以便为每个面板添加一个图形标题,并为整个图形添加一个副标题?

首先,不会添加额外的换行符。一、 如果你想有两行,不要用三个换行符(\n)。然后可以根据需要调整子块参数,为标题留出空间。E、 g.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)

。。。或使用受约束的布局:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.random(size=100)
f, a = plt.subplots(2, 2, figsize=(10, 5), constrained_layout=True)

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

enter image description here

相关问题 更多 >