ใน 3D scatter ใช้พารามิเตอร์ s ใน ax.scatter() กำหนดขนาดเครื่องหมายแต่ละจุดแยกกันได้ โดยคำนวณจากข้อมูล (เช่น s = zs / (ys*0.01)^2) และสีจากค่า x ด้วย colormap
ขั้นตอนการทำงาน
- ตั้งค่าขนาดรูปและเลย์เอาต์
- สร้างข้อมูล xs, ys, zs แบบสุ่ม
- คำนวณขนาดเครื่องหมาย s จากข้อมูล
- สร้าง figure และ axes 3D
- พลอต scatter 3D ด้วย
ax.scatter(ys, zs, xs, s=s*5, c=xs, cmap='copper') - แสดงผล
ตัวอย่างโค้ด
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
xs = np.random.randint(low=8, high=30, size=35)
ys = np.random.randint(130, 195, 35)
zs = np.random.randint(30, 160, 35)
s = zs / ((ys * 0.01) ** 2)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(ys, zs, xs, s=s * 5, c=xs, cmap='copper')
plt.show()
ผลลัพธ์
