如何將文字置於 Python 圖表外?


若要將文字置於圖表外,我們可以透過更改 text_pos_xtext_pos_y 的值來更改文字位置

步驟

  • 為 x 和 y 建立資料點。
  • 初始化 x 和 y 的文字位置。
  • 若要繪製 x 和 y,請使用帶 color='red' 的 plot() 方法。
  • 使用 text() 方法將文字新增到圖形。
  • 若要顯示圖形,請使用 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, 5, 100)
y = np.exp(x)
text_pos_x = 0.60
text_pos_y = 0.50
plt.plot(x, y, c='red')
plt.text(text_pos_x, text_pos_y, "$\mathit{y}=e^{x}$", fontsize=14,
transform=plt.gcf().transFigure, color='green')
plt.show()

輸出

更新於: 06-5-2021

3K+ 瀏覽次數

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.