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

การสร้าง Quiver Plot ใน Polar Coordinates ด้วย Matplotlib

Quiver Plot แสดง Vector Field ในระบบพิกัด Polar (R, θ) ใช้ ax.quiver() บน Polar Axes

ขั้นตอน

  1. ตั้งค่าขนาด Figure
  2. สร้างข้อมูล Radii และ Thetas
  3. สร้าง Meshgrid
  4. สร้าง Figure และ Polar Subplot
  5. วาด Quiver Plot
  6. แสดงผล

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


import numpy as np
from matplotlib import pyplot as plt

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

radii = np.linspace(0, 1, 5)
thetas = np.linspace(0, 2 * np.pi, 20)
theta, r = np.meshgrid(thetas, radii)

f = plt.figure()
ax = f.add_subplot(polar=True)

# U, V components ของ Vector
ax.quiver(theta, r, np.cos(theta) - np.sin(theta), 
          np.sin(theta) + np.cos(theta))

plt.show()

คำอธิบายพารามิเตอร์ quiver


ax.quiver(X, Y, U, V, **kwargs)
# X, Y: ตำแหน่งจุดเริ่มต้น (ใน polar: theta, r)
# U, V: ความยาว Vector ในทิศทาง X, Y (ใน polar: r, theta components)

ตัวอย่างเพิ่มเติม: Vector Field แบบ Spiral


ax.quiver(theta, r, -r * np.sin(theta), r * np.cos(theta))
# หรือใช้ scale เพื่อปรับขนาดลูกศร
ax.quiver(theta, r, U, V, scale=20, color='red')

ผลลัพธ์

Quiver Plot แบบ Polar แสดงลูกศรจำนวนมากจัดเรียงในรูปวงกลม แสดง Vector Field