如何在Python Matplotlib中为曲线设置标题?
要在Python Matplotlib中为曲线设置标题,可以按照以下步骤进行操作−
步骤
- 设置图形大小并调整子图之间和周围的填充。
-
创建 x 和 y 数据点,使曲线成为曲线。
-
绘制 x 和 y 数据点。
-
使用 plt.title() 方法为曲线图放置标题。
-
使用 Show() 方法显示图形。
示例
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# 创建x和y数据点
x = np.linspace(-1, 1, 50)
y = 2 ** x + 1
# 绘制x和y数据点
line, = plt.plot(x, y)
# 在图上放置标题
plt.title("y = 2 ^ x + 1")
plt.show()
输出
它将生成以下输出−