绘制半径为 R 的球体

Sim*_*hal 4 python matplotlib surface matplotlib-3d

我们怎样才能使半径为 R 的球体以给定坐标(x,y,z)为中心。就像有 10 组球心坐标和相应的 10 个不同的半径值一样。我们如何在 python 中绘制它?有什么方法可以在 python 中执行此操作,就像在 MATLAB 中一样,可以使用 surf 命令来完成此操作。在Python中我们如何实现这一点?

Law*_*tre 6

这将解决你的问题

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

list_center = [(1,2,3),(-4,-5,6), (5,5,6)]
list_radius = [1,2,1]

def plt_sphere(list_center, list_radius):
  for c, r in zip(list_center, list_radius):
    ax = fig.add_subplot(projection='3d')
    
    # draw sphere
    u, v = np.mgrid[0:2*np.pi:50j, 0:np.pi:50j]
    x = r*np.cos(u)*np.sin(v)
    y = r*np.sin(u)*np.sin(v)
    z = r*np.cos(v)

    ax.plot_surface(x-c[0], y-c[1], z-c[2], color=np.random.choice(['g','b']), alpha=0.5*np.random.random()+0.5)
fig = plt.figure()
plt_sphere(list_center, list_radius) 
Run Code Online (Sandbox Code Playgroud)

曲面图