Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตั้งตำแหน่ง Minor Ticks ใน Matplotlib


เพื่อตั้งตำแหน่ง Minor Ticks ใน Matplotlib

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้างข้อมูล x และ y ด้วย numpy
  • สร้าง figure และ subplots
  • พลอต x และ y ด้วย plot()
  • ตั้งตำแหน่ง minor ticks ด้วย set_minor_locator()
  • แสดง minor ticks ด้วย grid(which='minor')
  • แสดงรูปด้วย show()

ตัวอย่าง

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()

ผลลัพธ์

วิธีตั้งตำแหน่ง Minor Ticks ใน Matplotlib