如何在Matplotlib中为子图添加标题?

Sha*_*len 181 python plot matplotlib subtitle

我有一个包含许多子图的图.

fig = plt.figure(num=None, figsize=(26, 12), dpi=80, facecolor='w', edgecolor='k')
fig.canvas.set_window_title('Window Title')

# Returns the Axes instance
ax = fig.add_subplot(311) 
ax2 = fig.add_subplot(312) 
ax3 = fig.add_subplot(313) 
Run Code Online (Sandbox Code Playgroud)

如何向子图添加标题?

fig.suptitle为所有图形添加标题,虽然ax.set_title()存在,但后者不会为我的子图添加任何标题.

谢谢您的帮助.

编辑:纠正错误set_title().谢谢Rutger Kassies

小智 213

ax.set_title() 应该为单独的子图设置标题:

import matplotlib.pyplot as plt

if __name__ == "__main__":
    data = [1, 2, 3, 4, 5]

    fig = plt.figure()
    fig.suptitle("Title for whole figure", fontsize=16)
    ax = plt.subplot("211")
    ax.set_title("Title for first plot")
    ax.plot(data)

    ax = plt.subplot("212")
    ax.set_title("Title for second plot")
    ax.plot(data)

    plt.show()
Run Code Online (Sandbox Code Playgroud)

你能检查一下这段代码是否适合你吗?也许以后会覆盖它们?

  • 在 matplotlib 3.5.2 上,这会在 plt.subplot("211") / plt.subplot("212") 上抛出 ValueError 。解决方案是仅替换为整数参数 plt.subplot(211) 和 plt.subplot(212)。 (2认同)

Jar*_*rad 130

ax.title.set_text('My Plot Title') 似乎也有效.

fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')
plt.show()
Run Code Online (Sandbox Code Playgroud)

matplotlib在子图上添加标题

  • 如果您需要能够指定字体大小,请改用“ax.set_title('title', fontsize=16)”。 (5认同)

JMD*_*MDE 31

简写回答假设 import matplotlib.pyplot as plt:

plt.gca().set_title('title')
Run Code Online (Sandbox Code Playgroud)

如:

plt.subplot(221)
plt.gca().set_title('title')
plt.subplot(222)
etc...
Run Code Online (Sandbox Code Playgroud)

然后就不需要多余的变量了.


小智 11

如果你想让它更短,你可以写:

import matplolib.pyplot as plt
for i in range(4):
    plt.subplot(2,2,i+1).set_title('Subplot n°{}' .format(i+1))
plt.show()
Run Code Online (Sandbox Code Playgroud)

它使它可能不太清楚,但您不需要更多的行或变量


JMD*_*MDE 8

我越来越倾向于使用的一种解决方案是:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)  # 1
for i, ax in enumerate(axs.ravel()): # 2
    ax.set_title("Plot #{}".format(i)) # 3
Run Code Online (Sandbox Code Playgroud)
  1. 创建任意数量的轴
  2. axs.ravel() 将您的 2-dim 对象转换为 row-major 样式的 1-dim 矢量
  3. 将标题分配给当前轴对象


Woj*_*ski 6

fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1, ncols=4,figsize=(11, 7))

grid = plt.GridSpec(2, 2, wspace=0.2, hspace=0.5)

ax1 = plt.subplot(grid[0, 0])
ax2 = plt.subplot(grid[0, 1:])
ax3 = plt.subplot(grid[1, :1])
ax4 = plt.subplot(grid[1, 1:])

ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


div*_*nex 6

为了完整性,也可以在不显式引用图形轴的情况下实现所请求的结果,如下所示:

import matplotlib.pyplot as plt

plt.subplot(221)
plt.title("Title 1")

plt.subplot(222)
plt.title("Title 2")

plt.subplot(223)
plt.title("Title 3")

plt.subplot(224)
plt.title("Title 4")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

plt.tight_layout()如果您遇到标签重叠问题,请在最后一个图之后使用。