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

Cách gán hàm pyplot vào instance figure Matplotlib


Để sử dụng interface hướng đối tượng (OO), tạo figure bằng plt.figure(), thêm axes bằng fig.add_subplot(), sau đó gọi phương thức trên axes.

Các bước

  • Tạo figure.
  • Thêm axes.
  • Đặt tiêu đề axes bằng ax.set_title().
  • Hiển thị.

Ví dụ minh họa

import matplotlib.pyplot as plt

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

fig = plt.figure()
ax = fig.add_subplot()
ax.set_title("Tiêu đề của tôi!")
plt.show()

Kết quả đầu ra

Cách gán hàm pyplot vào instance figure Matplotlib