Đặt xticks và xticklabels tùy chỉnh để hiển thị giá trị mong muốn trên trục X.
- x = [45, 1, 34, 78, 100], y = [8, 10, 23, 78, 2].
- default_x_ticks = range(len(x)).
- plt.plot(default_x_ticks, y).
- plt.xticks(default_x_ticks, x) gán nhãn x cho các vị trí 0..4.
- show().
Ví dụ
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [45, 1, 34, 78, 100] y = [8, 10, 23, 78, 2] default_x_ticks = range(len(x)) plt.plot(default_x_ticks, y) plt.xticks(default_x_ticks, x) plt.show()
Kết quả
Đồ thị với trục X vị trí 0-4 nhưng nhãn là 45, 1, 34, 78, 100.