我正在编写用于绘制球体的代码。我使用单独的函数生成随机点,我想将这些作为球体添加到同一图表中。目前,为每个球体创建了单独的图形。代码如下所示。这里的磁盘是[x,y,z]形式的列表。另外,是否可以使x,y和z轴的长度相等?非常感谢。
def plot_disks2(disk, radius, c, ax=None):
fig = plt.figure(figsize=(12,12), dpi=300)
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = radius * np.outer(np.cos(u), np.sin(v))
y = radius * np.outer(np.sin(u), np.sin(v))
z = radius * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x+disk[0], y+disk[1], z+disk[2], rstride=4, cstride=4, color=c, linewidth=0, alpha=0.5)
答案 0 :(得分:0)
简而言之,建议您使用函数生成数据,使用其他函数进行绘图,如下所示:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def disks2(disk, radius):
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = radius * np.outer(np.cos(u), np.sin(v))
y = radius * np.outer(np.sin(u), np.sin(v))
z = radius * np.outer(np.ones(np.size(u)), np.cos(v))
return x+disk[0],y+disk[1],z+disk[2]
def plotting_spheres(data, colors):
fig = plt.figure(figsize=(12,12), dpi=300)
ax = fig.add_subplot(111, projection='3d')
for k,sph in enumerate(data):
x, y, z = sph[0], sph[1], sph[2]
ax.plot_surface(x, y, z, rstride=4, cstride=4,
color=colors[k], linewidth=0, alpha=0.5)
ax.set_aspect("equal")
plt.show()
N = 5 # spheres
points = np.random.randint(1,10,(N,3))
rs = np.random.random((N,1))
colors = ("r","g","b","y","m")
data = [disks2(points[k,:], rs[k]) for k in range(N)]
plotting_spheres(data, colors)
对于轴方面,您可以使用set_aspect
方法。