Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách đặt bước (step) trên trục X trong Matplotlib Python 2.6.6

Đặt xticks và xticklabels tùy chỉnh để kiểm soát bước hiển thị trên trục X.

  • Tạo dữ liệu x, y.
  • Vẽ plt.plot(x, y).
  • ax1 = plt.subplot(), ax1.set_xticks(x).
  • ax1.set_xticklabels(["one","two","three","four"], rotation=45).
  • Hiển thị bằng 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]
y = [1.2, 1.9, 3.1, 4.2]
plt.plot(x, y)
ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_xticklabels(["one", "two", "three", "four"], rotation=45)
plt.show()

Kết quả

Biểu đồ với trục X có ticks tại 1,2,3,4 và nhãn one, two, three, four xoay 45 độ.