Để truy cập đối tượng nhãn trục trong Matplotlib, ta dùng ax.xaxis.get_label().get_text().
Các bước
- Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
- Tạo figure và subplots.
- Khởi tạo N số mẫu.
- Tạo dữ liệu ngẫu nhiên numpy.
- Vẽ x bằng plot().
- Đặt nhãn trục X bằng set_xlabel().
- Lấy xlabel bằng get_label() và get_text().
- Hiển thị hình bằng show().
Ví dụ
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
N = 100
x = np.random.rand(N)
ax.plot(x)
ax.set_xlabel("X-axis")
x_lab = ax.xaxis.get_label()
print("Label is: ", x_lab.get_text())
plt.show()
Kết quả
