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

การพลอต Scatter 3D ขนาดเครื่องหมายเปลี่ยนแปลงได้ใน Matplotlib

ใน 3D scatter ใช้พารามิเตอร์ s ใน ax.scatter() กำหนดขนาดเครื่องหมายแต่ละจุดแยกกันได้ โดยคำนวณจากข้อมูล (เช่น s = zs / (ys*0.01)^2) และสีจากค่า x ด้วย colormap

ขั้นตอนการทำงาน

  1. ตั้งค่าขนาดรูปและเลย์เอาต์
  2. สร้างข้อมูล xs, ys, zs แบบสุ่ม
  3. คำนวณขนาดเครื่องหมาย s จากข้อมูล
  4. สร้าง figure และ axes 3D
  5. พลอต scatter 3D ด้วย ax.scatter(ys, zs, xs, s=s*5, c=xs, cmap='copper')
  6. แสดงผล

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

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()

ผลลัพธ์

การพลอต Scatter 3D ขนาดเครื่องหมายเปลี่ยนแปลงได้ใน Matplotlib