如何旋轉 Matplotlib 註解以與線匹配?


要旋轉 matplotlib 註解以匹配一條線,我們可以採取以下步驟 -

  • 使用 figure() 方法建立一個新圖形或啟用一個現有的圖形。
  • 使用 add_subplot() 方法將一個 '~.axes.Axes' 新增到圖形中作為子圖佈局的一部分。
  • 初始化變數,m(斜率)和 c(截距)。
  • 使用 numpy 建立 x 和 y 資料點。
  • 計算 theta 以進行文字旋轉。
  • 使用 plot() 方法並使用 x 和 y繪製線條。
  • 使用 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
fig = plt.figure()
ax = fig.add_subplot()
m = 1
c = 1
x = np.linspace(-2, 2, 10)
y = m*x + c
theta = np.arctan(m)
line, = ax.plot(x, y)
ax.text(x=x[2], y=y[2]+.25, s="y=mx+c", rotation=180.0*(1-theta), fontsize=15, color='green')
plt.show()

輸出

更新於: 2021 年 5 月 15 日

3K+ 瀏覽

啟動你的職業生涯

透過完成該課程獲得認證

開始
廣告
© . All rights reserved.