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

Đặt vị trí minor ticks trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo dữ liệu x, y bằng numpy.
  3. Tạo figure và subplots.
  4. Vẽ đồ thị bằng plot().
  5. Đặt vị trí minor ticks bằng set_minor_locator() (ví dụ AutoMinorLocator(2)).
  6. Hiển thị minor grid bằng grid(which='minor').
  7. Gọi show().

Ví dụ

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.linspace(1, 10, 100)
y = np.log(x)
fig, ax = plt.subplots()
ax.plot(x, y)
minor_locator = AutoMinorLocator(2)
ax.xaxis.set_minor_locator(minor_locator)
plt.grid(which='minor')
plt.show()

Kết quả

Đặt vị trí minor ticks trong Matplotlib