Để chỉ hiển thị nhãn text trong legend, ta dùng legend() với handlelength=0, handletextpad=0, fancybox=0.
Các bước thực hiện
- Thiết lập kích thước hình và khoảng đệm.
- Tạo x, y ngẫu nhiên bằng NumPy.
- Tạo hình và subplots bằng subplots().
- Vẽ x, y với label "Zig-Zag".
- Gọi legend() với handlelength=0, handletextpad=0, fancybox=True.
- Hiển thị bằng show().
Ví dụ
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.rand(100)
y = np.random.rand(100)
fig, ax = plt.subplots()
ax.plot(x, y, label='Zig-Zag', c="red")
ax.legend(loc="upper right", handlelength=0, handletextpad=0, fancybox=True)
plt.show()
Kết quả
