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

Matplotlib – Vẽ lên nền ảnh trong Python

Để vẽ lên nền ảnh:

  • Đọc ảnh từ file vào mảng.
  • Tạo hình (fig) và subplot (ax) với extent [0, 300, 0, 300].
  • Tạo mảng x range(300).
  • Vẽ x bằng plot() với linestyle=dotted, linewidth=2, color=red.
  • Hiển thị bằng 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
im = plt.imread("bird.jpg")
fig, ax = plt.subplots()
im = ax.imshow(im, extent=[0, 300, 0, 300])
x = np.array(range(300))
ax.plot(x, x, ls='dotted', linewidth=2, color='red')
plt.show()

Kết quả

Matplotlib – Vẽ lên nền ảnh trong Python