📌  相关文章
📜  如何在 Matplotlib 中为子图添加标题?

📅  最后修改于: 2022-05-13 01:54:21.460000             🧑  作者: Mango

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

在本文中,我们将看到如何在 Matplotlib 中为子图添加标题?让我们讨论一些概念:

  • Matplotlib : Matplotlib 是一个了不起的Python可视化库,用于二维数组绘图。 Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。它是由 John Hunter 在 2002 年推出的。
  • Subplots : matplotlib 库的 pyplot 模块中的 subplots()函数用于创建图形和一组子图。当我们想在同一图中显示两个或多个图时,需要子图。
  • 绘图标题: matplotlib 模块中的 title() 方法用于指定所描绘的可视化的标题,并使用各种属性显示标题。

需要的步骤

  • 导入库
  • 创建/加载数据
  • 制作子图
  • 绘制子图
  • 将标题设置为子图。

示例 1:(使用 set_title() 方法)

我们使用 matplotlib.axes._axes.Axes.set_title(label) 方法为当前子图轴设置标题(字符串标签)。

Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)
  
# set the title to subplots
ax[0, 0].set_title("Linear")
ax[0, 1].set_title("Double")
ax[1, 0].set_title("Square")
ax[1, 1].set_title("Cube")
  
# set spacing
fig.tight_layout()
plt.show()


Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)
  
# set the title to subplots
ax[0, 0].title.set_text("Linear")
ax[0, 1].title.set_text("Double")
ax[1, 0].title.set_text("Square")
ax[1, 1].title.set_text("Cube")
  
# set spacing
fig.tight_layout()
plt.show()


Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
title = ["Linear", "Double", "Square", "Cube"]
y = [x, x*2, x*x, x*x*x]
  
for i in range(4):
        
    # subplots
    plt.subplot(2, 2, i+1) 
      
    # ploting (x,y)
    plt.plot(x, y[i]) 
      
    # set the title to subplots
    plt.gca().set_title(title[i]) 
  
# set spacing
fig.tight_layout()
plt.show()


Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
title = ["Linear","Double","Square","Cube"]
y = [x, x*2, x*x, x*x*x]
  
for i in range(4):
        
    # subplots
    plt.subplot(2, 2, i+1)
      
    # ploting (x,y)
    plt.plot(x, y[i])
      
    # set the title to subplots
    plt.gca().title.set_text(title[i]) 
  
# set spacing
fig.tight_layout()
plt.show()


输出:

示例 2:(使用 title.set_text() 方法)

我们还可以使用 title.set_text() 方法为 Matplotlib 中的子图添加标题,与 set_title() 方法类似。

蟒蛇3

# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)
  
# set the title to subplots
ax[0, 0].title.set_text("Linear")
ax[0, 1].title.set_text("Double")
ax[1, 0].title.set_text("Square")
ax[1, 1].title.set_text("Cube")
  
# set spacing
fig.tight_layout()
plt.show()

输出:

示例 3:(使用 plt.gca().set_title() 方法)

如果您在交互式绘图中使用类似 Matlab 的样式,那么您可以使用 plt.gca() 获取子图当前轴的参考,并结合 set_title() 方法为 Matplotlib 中的子图设置标题。

蟒蛇3

# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
title = ["Linear", "Double", "Square", "Cube"]
y = [x, x*2, x*x, x*x*x]
  
for i in range(4):
        
    # subplots
    plt.subplot(2, 2, i+1) 
      
    # ploting (x,y)
    plt.plot(x, y[i]) 
      
    # set the title to subplots
    plt.gca().set_title(title[i]) 
  
# set spacing
fig.tight_layout()
plt.show()

输出 :

示例 4:(使用 plt.gca().title.set_text() 方法)

如果您在交互式绘图中使用类似 Matlab 的样式,那么您可以使用 plt.gca() 获取子图当前轴的参考,并结合 title.set_text() 方法将标题设置为 Matplotlib 中的子图。

蟒蛇3

# importing packages
import numpy as np
import matplotlib.pyplot as plt
  
# create data
x=np.array([1, 2, 3, 4, 5])
  
# making subplots
fig, ax = plt.subplots(2, 2)
  
# set data with subplots and plot
title = ["Linear","Double","Square","Cube"]
y = [x, x*2, x*x, x*x*x]
  
for i in range(4):
        
    # subplots
    plt.subplot(2, 2, i+1)
      
    # ploting (x,y)
    plt.plot(x, y[i])
      
    # set the title to subplots
    plt.gca().title.set_text(title[i]) 
  
# set spacing
fig.tight_layout()
plt.show()

输出 :