Để vẽ hình ảnh watermark trong Matplotlib:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
- Lấy file dữ liệu mẫu bằng get_sample_data().
- Tạo figure và tập subplots.
- Vẽ điểm dữ liệu bằng plot() với alpha=0.7 và mfc="orange".
- Thêm hình ảnh không resample vào figure.
- Hiển thị hình bằng show().
Ví dụ
import numpy as np
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
with cbook.get_sample_data('logo2.png') as file:
im = image.imread(file)
fig, ax = plt.subplots()
ax.plot(np.sin(10 * np.linspace(0, 1)), '-o', ms=20,
alpha=0.7, mfc='orange')
fig.figimage(im, 10, 10, zorder=3, alpha=.5)
plt.show()
Kết quả
