Matplotlib – plt.subplots() 和 plt.figure() 之间的区别

plt.figure()− 创建新图形或激活现有图形。

plt.subplots()- 创建一个图形和一组子图。

让我们举个例子来了解和之间的区别。plt.subplots()plt.figure()

脚步

  • 设置图形大小并调整子图之间和周围的填充。

  • 创建新地物或激活现有地物。使用方法。plt.figure()

  • 创建一个图形和一组子图。使用方法。plt.subplots()

  • 要显示图形,请使用Show()方法。

示例

from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a new figure using plt.figure
fig1 = plt.figure("Figure 1 - plt.figure")

# Create a figure and set of subplots using plt.subplots
fig2, ax = plt.subplots()
plt.title("Figure 2 - plt.subplots")

# Display the plot
plt.show()
输出结果

plt.figure()产生以下输出

并且,产生以下输出 -plt.subplots()