เพื่อสร้างกราฟ 4 มิติ สามารถใช้ข้อมูล x, y, z และ c (สี) เป็นมิติที่ 4 ได้
ขั้นตอน
- ใช้เมธอด figure() เพื่อสร้าง Figure ใหม่หรือเปิดใช้งาน Figure ที่มีอยู่
- เพิ่ม Figure เข้า subplot arrangement
- สร้างข้อมูล x, y, z และ c ด้วย NumPy (ข้อมูลสุ่มมาตรฐาน)
- สร้าง Scatter Plot 3D ด้วยเมธอด scatter() โดยใช้ c เป็นค่าสี
- แสดงกราฟด้วยเมธอด show()
ตัวอย่างโค้ด
from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.random.standard_normal(100) y = np.random.standard_normal(100) z = np.random.standard_normal(100) c = np.random.standard_normal(100) img = ax.scatter(x, y, z, c=c, cmap='YlOrRd', alpha=1) plt.show()
ผลลัพธ์
