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

Cách vẽ đồ thị 3D bằng Python Matplotlib

Vẽ scatter 3D cơ bản với Axes3D.

  • from mpl_toolkits.mplot3d import Axes3D.
  • fig = plt.figure(), ax = Axes3D(fig).
  • Tạo lists x, y, z.
  • ax.scatter3D(x, y, z, c=z, alpha=1, marker='d', s=150).
  • show().

Ví dụ

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()

Kết quả

Đồ thị.scatter 3D 5 điểm kim cương (diamond) màu theo giá trị z.