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

วิธีจัดตำแหน่งป้ายแกนไปทางขวา (X) หรือบน (Y) ใน Matplotlib


เพื่อจัดป้ายแกน X ไปทางขวา และป้ายแกน Y ไปด้านบน สามารถทำได้ดังนี้

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้าง figure และ subplots
  • กำหนดตัวแปร N สำหรับจำนวนข้อมูล
  • พลอต x และ y ด้วย plot()
  • ตั้งค่า xlabel และ ylabel ที่ตำแหน่ง right และ top ตามลำดับ
  • แสดงรูปด้วย show()

ตัวอย่าง

import numpy as np
from matplotlib import pyplot as plt

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

fig, ax = plt.subplots()
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
ax.plot(x, y)
ax.set_xlabel("X-axis", loc="right")
ax.set_ylabel("Y-axis", loc="top")
plt.show()

ผลลัพธ์

วิธีจัดตำแหน่งป้ายแกนไปทางขวา (X) หรือบน (Y) ใน Matplotlib