如何在 Python plt.title 中新增變數?


若要向 Python plt.title() 新增變數,我們可以採取以下步驟 −

  • 使用 numpy 和 num(是一個變數)建立 x 和 y 的資料點,以計算 y 並將其設定為標題。

  • 使用 plot() 方法以紅色繪製 x 和 y 資料點。

  • 使用變數 num 設定曲線的標題。

  • 若要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-1, 1, 10)
num = 2
y = num ** x
plt.plot(x, y, c='red')
plt.title(f"y=%d$^x$" % num)
plt.show()

輸出

更新時間: 08-5-2021

1.1 萬+

啟動您的 職業生涯

完成課程獲得認證

開始使用
廣告
© . All rights reserved.