Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách dùng pyplot.arrow hoặc patches.Arrow trong matplotlib?


Để dùng pyplot.arrow hoặc patches.Arrow() trong matplotlib, chúng ta có thể thực hiện các bước sau:

  • Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
  • Khởi tạo bốn biến: x_tail, y_tail, x_head và y_head.
  • Tạo một figure và một tập subplots.
  • Lấy instance fancy arrow.
  • Thêm artist (bước 4) bằng phương thức add_patch().
  • Để hiển thị figure, dùng phương thức show().

Ví dụ

from matplotlib import pyplot as plt, patches as mpatches

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x_tail = 0.1
y_tail = 0.1
x_head = 0.9
y_head = 0.9

fig, ax = plt.subplots()
arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (x_head, y_head), mutation_scale=100, color='green')
ax.add_patch(arrow)

plt.show()

Kết quả

Cách dùng pyplot.arrow hoặc patches.Arrow trong matplotlib?Cách dùng pyplot.arrow hoặc patches.Arrow trong matplotlib?