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

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


เพื่อปรับแต่งป้ายกำกับ X-axis สามารถทำตามขั้นตอนดังนี้

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • กำหนดตัวแปร N สำหรับจำนวนข้อมูลตัวอย่าง
  • สร้างข้อมูล x และ y ด้วย numpy
  • พลอต x และ y ด้วยเมธอด plot()
  • ปรับแต่งป้าย X-axis ด้วย fontweight, color, fontsize, alignment
  • แสดงรูปด้วย show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt

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

N = 100
x = np.random.rand(N)
y = np.random.rand(N)

plt.plot(x, y, 'r*')
plt.xlabel('X-axis Label', fontweight='bold', color='orange', fontsize='17', horizontalalignment='center')
plt.show()

ผลลัพธ์

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