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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25239933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:55:38  来源:igfitidea点击:

How to add title to subplots in Matplotlib?

pythonmatplotlibplotsubtitle

提问by Shailen

I have one figure which contains many subplots.

我有一个包含许多子图的图形。

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) 

How do I add titles to the subplots?

如何为子图添加标题?

fig.suptitleadds a title to all graphs and although ax.set_title()exists, the latter does not add any title to my subplots.

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

Thank you for your help.

感谢您的帮助。

Edit: Corrected typo about set_title(). Thanks Rutger Kassies

编辑:更正了关于set_title(). 感谢 Rutger Kassies

采纳答案by Jarad

ax.title.set_text('My Plot Title')seems to work too.

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

matplotlib add titles on subplots

matplotlib 在子图上添加标题

回答by masteusz

ax.set_title()should set the titles for separate subplots:

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

Can you check if this code works for you? Maybe something overwrites them later?

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

回答by JMDE

A shorthand answer assuming import matplotlib.pyplot as plt:

一个速记答案假设 import matplotlib.pyplot as plt

plt.gca().set_title('title')

as in:

如:

plt.subplot(221)
plt.gca().set_title('title')
plt.subplot(222)
etc...

Then there is no need for superfluous variables.

那么就不需要多余的变量了。

回答by SAGET Shinji

If you want to make it shorter, you could write :

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

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

It makes it maybe less clear but you don't need more lines or variables

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

回答by rishi jain

In case you have multiple images and you want to loop though them and show them 1 by 1 along with titles - this is what you can do. No need to explicitly define ax1, ax2, etc.

如果您有多个图像,并且想要遍历它们并将它们与标题一一显示 - 这就是您可以做的。无需明确定义 ax1、ax2 等。

  1. The catch is you can define dynamic axes(ax) as in Line 1 of code and you can set its title inside a loop.
  2. The rows of 2D array is length (len) of axis(ax)
  3. Each row has 2 items i.e. It is list within a list (Point No.2)
  4. set_title can be used to set title, once the proper axes(ax) or subplot is selected.
  1. 问题是您可以在代码的第 1 行中定义动态轴(ax),并且您可以在循环中设置其标题。
  2. 二维数组的行是轴(ax)的长度(len)
  3. 每行有 2 个项目,即它是列表中的列表(第 2 点)
  4. set_title 可用于设置标题,一旦选择了正确的轴(ax)或子图。
import matplotlib.pyplot as plt    
fig, ax = plt.subplots(2, 2, figsize=(6, 8))  
for i in range(len(ax)): 
    for j in range(len(ax[i])):
        ## ax[i,j].imshow(test_images_gr[0].reshape(28,28))
        ax[i,j].set_title('Title-' + str(i) + str(j))

回答by Wojciech Moszczyński

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

enter image description here

在此处输入图片说明