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

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

Matplotlib là thư viện vẽ đồ thị 2D/3D phổ biến, dựa trên NumPy, hỗ trợ IPython, Jupyter, Spyder.

Cài đặt

pip install matplotlib
# Phụ thuộc: Python≥3.4, NumPy, Setuptools, Pyparsing, libpng, pytz, freetype, six, cycler, python-dateutil

Ví dụ: Wireframe 3D


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

def my_fun(x, y):
    return np.sin(np.sqrt(x**4 + y**4))

x = np.linspace(-8, 8, 30)
y = np.linspace(-8, 8, 30)
X, Y = np.meshgrid(x, y)
Z = my_fun(X, Y)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='red')
plt.xlabel('X axis')
plt.ylabel('Y axis')
ax.set_title('Wireframe plot 3D')
plt.show()

Kết quả

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

Giải thích

  • meshgrid tạo lưới tọa độ 2D từ vector 1D.
  • plot_wireframe vẽ khung dây (chỉ đường viền) của mặt 3D.
  • projection='3d' kích hoạt axes 3D.