Tính góc xoay từ độ dốc đường thẳng (theta = arctan(m)) để xoay text song song.
- Tạo figure, ax = add_subplot().
- m = 1, c = 1, x = linspace(-2,2,10), y = m*x + c.
- theta = np.arctan(m).
- 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').
- show().
Ví dụ
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()
Kết quả
Đường thẳng y=x+1 với chú thích "y=mx+c" xoay song song với đường thẳng.