Để xóa chữ số sau dấu thập phân trên tick labels trục trong Matplotlib:
- Thiết lập kích thước figure và padding
- Tạo dữ liệu
x,ybằng numpy - Tạo figure và subplots
- Đặt
xtick labelschỉ số nguyên bằngx.astype(int) - Hiển thị bằng
show()
Ví dụ mã nguồn
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1.110, 2.110, 4.110, 5.901, 6.00, 7.90, 8.90]) y = np.array([2.110, 1.110, 3.110, 9.00, 4.001, 2.095, 5.890]) fig, ax = plt.subplots() ax.plot(x, y) ax.set_xticklabels(x.astype(int)) plt.show()
Kết quả
Đồ thị với trục X hiển thị tick labels là số nguyên: 1, 2, 4, 5, 6, 7, 8.

Cách khác:
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: int(x)))ax.set_xticks(x); ax.set_xticklabels([f'{v:.0f}' for v in x])- Dùng
MaxNLocator(integer=True)để tự động chỉ lấy tick số nguyên