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

Vẽ biểu đồ mặt nạ (masked surface plot) dùng Python, NumPy và Matplotlib

Để vẽ masked surface plot, thực hiện:

  • Thiết lập kích thước hình và padding.
  • Tạo figure mới hoặc kích hoạt figure hiện có.
  • Thêm 'ax' vào figure như subplot.
  • Tạo ma trận tọa độ từ vector pi, theta.
  • Tạo x, y, z với dữ liệu masked.
  • Vẽ surface plot với x, y, z.
  • Hiển thị bằng show().

Ví dụ

import matplotlib.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")

pi, theta = np.meshgrid(
    np.arange(1, 10, 2) * np.pi / 4,
    np.arange(1, 10, 2) * np.pi / 4
)

x = np.cos(pi) * np.sin(theta)
y = np.sin(pi) * np.sin(theta)
z = np.ma.masked_where(x >= 0.01, y)

ax.plot_surface(x, y, z, color='red')
plt.show()

Đầu ra

Sẽ tạo ra đầu ra:

Vẽ biểu đồ mặt nạ (masked surface plot) dùng Python, NumPy và Matplotlib
Vẽ biểu đồ mặt nạ (masked surface plot) dùng Python, NumPy và Matplotlib