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

วิธีวาดกราฟ 3D ด้วย Python Matplotlib


สร้างกราฟ 3 มิติพื้นฐาน

ใช้ mpl_toolkits.mplot3d และ scatter3D

ขั้นตอน

  1. สร้าง figure ด้วย figure()
  2. ดึง axes 3D object
  3. สร้าง list x, y, z สำหรับจุดข้อมูล
  4. เพิ่ม 3D scatter points ด้วย scatter3D() markersize=150, marker=diamond
  5. แสดงด้วย show()

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

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt

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

fig = plt.figure()
ax = Axes3D(fig)

x = [2, 4, 6, 3, 1]
y = [1, 6, 8, 1, 3]
z = [3, 4, 10, 3, 1]

ax.scatter3D(x, y, z, c=z, alpha=1, marker='d', s=150)
plt.show()

ผลลัพธ์

วิธีวาดกราฟ 3D ด้วย Python Matplotlib