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

วิธีสร้าง 3D Line Plot ด้วย Matplotlib ใน Python

บทนำ

3D Line Plot แสดงเส้นโค้งในพื้นที่ 3 มิติ ใช้ plot3D แทน plot

การติดตั้ง

pip install matplotlib

Dependencies

Python>=3.4, NumPy, Setuptools, Pyparsing, Libpng, Pytz, FreeType, Six, Cycler, Dateutil

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

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes(projection='3d')

z = np.linspace(0, 1, 100)
x = z * np.sin(20 * z)
y = z * np.cos(20 * z)

ax.plot3D(x, y, z, 'red')
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.set_title('A sample 3D line plot')
plt.show()

คำอธิบาย

  • สร้างข้อมูล Spiral: x = z·sin(20z), y = z·cos(20z)

  • ใช้ ax.plot3D ระบุสีเส้น

  • ตั้งค่า Label 3 แกนและ Title