Giới thiệu
Để các xticks cách đều nhau bất kể giá trị thực, dùng set_ticks() và set_ticklabels().
Các bước
- Thiết lập kích thước hình và padding.
- Tạo dữ liệu
x,ybằng numpy. - Tạo figure và subplots (2 hàng, 1 cột).
- Trục 1: vẽ và set ticks bằng giá trị x thực.
- Trục 2: vẽ và set ticks bằng chỉ số (0,1,2...), set ticklabels bằng giá trị x.
- Gọi
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.array([1, 1.5, 3, 5, 6]) y = np.power(2, x) fig, (ax1, ax2) = plt.subplots(2, 1) ax1.plot(x, y) ax1.xaxis.set_ticks(x) ax2.plot(x, y) ax2.xaxis.set_ticks(range(len(x))) ax2.xaxis.set_ticklabels(x) plt.show()
Kết quả
