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

Vẽ điểm trên mặt cầu trong Python Matplotlib

Dùng plot_surface() với tọa độ cầu x=cos(u)sin(v), y=sin(u)sin(v), z=cos(v).

  • Tạo figure, ax = add_subplot(projection='3d').
  • r = 0.05 (không dùng?).
  • u, v = np.mgrid[0:2π:30j, 0:π:20j].
  • x, y, z từ công thức cầu.
  • ax.plot_surface(x, y, z, cmap=plt.cm.YlGnBu_r).
  • 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(projection='3d')
r = 0.05
u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j]
x = np.cos(u) * np.sin(v)
y = np.sin(u) * np.sin(v)
z = np.cos(v)
ax.plot_surface(x, y, z, cmap=plt.cm.YlGnBu_r)
plt.show()

Kết quả

Mặt cầu đơn vị được vẽ bằng surface plot với colormap YlGnBu_r.