Xoay nhãn tick (tick labels) trong subplot bằng set_xticklabels()/set_yticklabels() với tham số rotation.
- Tạo danh sách x cho ticks.
- Lấy trục bằng subplot().
- Đặt xticks, yticks bằng x.
- Đặt xticklabels, yticklabels với rotation=45.
- Dùng tick_params(axis="both", direction="in", pad=15) để điều chỉnh khoảng cách.
- Hiển thị bằng plt.show().
Ví dụ
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_yticks(x) ax1.set_xticklabels(["one", "two", "three", "four"], rotation=45) ax1.set_yticklabels(["one", "two", "three", "four"], rotation=45) ax1.tick_params(axis="both", direction="in", pad=15) plt.show()
Kết quả
Biểu đồ với nhãn tick trên cả hai trục xoay 45 độ, ticks hướng vào trong.