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

การพล็อต Scatter Points บน Polar Axis ใน Matplotlib

Scatter Plot บน Polar Coordinates

ใช้ projection='polar' ใน add_subplot() แล้ว scatter(theta, r)

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • กำหนด N จำนวนจุด
  • สร้าง r, theta, area, color ด้วย numpy random
  • สร้าง figure และ polar axes
  • พล็อต scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
  • แสดงผลด้วย show()

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

import numpy as np
import matplotlib.pyplot as plt

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

N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
plt.show()

คำอธิบาย

  • theta: มุม (radians)
  • r: รัศมี
  • area: ขนาดจุด (สัมพันธ์กับ r^2)
  • cmap='hsv': สีตามมุม theta