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

การสร้างกราฟ 3D จากอาร์เรย์ NumPy 3 มิติใน Matplotlib


สร้าง 3D plot จากอาร์เรย์ NumPy 3 มิติ

สร้างอาร์เรย์ 3 มิติ แล้วแยกจุด x, y, z มาวาด scatter plot

  • สร้าง figure ด้วย figure()
  • เพิ่ม axes 3D ด้วย add_subplot(projection='3d')
  • สร้างข้อมูลสุ่ม size=(3, 3, 3)
  • แยก x, y, z จากอาร์เรย์ 3D
  • วาด 3D scattered points
  • แสดงด้วย show()

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

import numpy as np
from matplotlib import pyplot as plt

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

data = np.random.random(size=(3, 3, 3))
z, x, y = data.nonzero()
ax.scatter(x, y, z, c=z, alpha=1)

plt.show()

ผลลัพธ์

การสร้างกราฟ 3D จากอาร์เรย์ NumPy 3 มิติใน Matplotlib