Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo line plot 3D với Matplotlib trong Python

Line plot 3D vẽ đường cong tham số trong không gian 3 chiều (x(t), y(t), z(t)).

Cài đặt

pip install matplotlib

Ví dụ: Đường xoắn ốc 3D


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('3D Line Plot')
plt.show()

Kết quả

Tạo line plot 3D với Matplotlib trong Python

Giải thích

  • plot3D vẽ đường nối các điểm 3D theo thứ tự.
  • Dữ liệu tham số tạo đường xoắn ốc mở rộng theo z.
  • Dùng cho trực quan hóa quỹ đạo, gradient descent path.