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

วิธีปรับแต่ง X-axis ticks ใน Matplotlib


เพื่อปรับแต่ง X-axis ticks ใน Matplotlib สามารถเปลี่ยนความยาวและความกว้างของ ticks ได้

ขั้นตอน

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้างลิสต์สำหรับข้อมูล height, bars และ y_pos
  • สร้าง bar plot ด้วยเมธอด bar()
  • ปรับแต่ง X-axis ticks ด้วยเมธอด tick_params() กำหนด color=red, direction=outward, length=7, width=2
  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt

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

height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))

plt.bar(y_pos, height, color='yellow')
plt.tick_params(axis='x', colors='red', direction='out', length=7, width=2)
plt.show()

ผลลัพธ์

วิธีปรับแต่ง X-axis ticks ใน Matplotlib