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

Cách giảm mật độ nhãn tick trong subplots Matplotlib

Giảm số lượng ticks bằng cách đặt xticks với ít điểm hơn (biến density).

  • density = 10.
  • x = np.linspace(-2, 2, density).
  • y = np.sin(x).
  • plt.plot(x, y).
  • plt.xticks(x) đặt ticks chính xác tại các điểm x.
  • show().

Ví dụ

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
density = 10
x = np.linspace(-2, 2, density)
y = np.sin(x)
plt.plot(x, y)
plt.xticks(x)
plt.show()

Kết quả

Đồ thị sin(x) với 10 ticks trên trục X, nhãn hiển thị tại mỗi điểm dữ liệu.