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

วิธีลดความหนาแน่นของ tick labels ใน subplots ด้วย Matplotlib


ลดจำนวน tick labels บนแกน

กำหนดค่า density ต่ำเพื่อลดจำนวน ticks

ขั้นตอน

  1. กำหนดตัวแปร density
  2. สร้างข้อมูล x, y ด้วย NumPy
  3. วาดกราฟด้วย plot()
  4. ตั้งค่า tick locations และ labels ด้วย xticks()
  5. แสดงด้วย show()

ตัวอย่างโค้ด

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

ผลลัพธ์

วิธีลดความหนาแน่นของ tick labels ใน subplots ด้วย Matplotlib