如何在 Python Matplotlib 中給曲線圖新增標題?


在 Python Matplotlib 中給曲線圖加標題,我們可以採用以下步驟 -

步驟

  • 設定圖的大小並調整子圖之間的填充。

  • 建立 xy 資料點,讓影像變成一條曲線。

  • 繪製 xy 資料點。

  • 使用 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

# Create x and y data points
x = np.linspace(-1, 1, 50)
y = 2**x + 1

# Plot the x and y data points
line, = plt.plot(x, y)

# Place a title on the plot
plt.title("$y=2^x+1$")

plt.show()

輸出

它將產生以下輸出 -

更新於: 11-Oct-2021

529 次瀏覽

開啟你的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.