在球体内创建球体

时间:2014-11-05 22:57:54

标签: python matplotlib geometry

是否可以创建像这样的图形(我不需要注释,也不需要线条......只有球体的图层[填充。])

enter image description here

使用matplotlib?

修改

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




fig = plt.figure()



ax = fig.add_subplot(111, projection='3d')



r=2

u = np.linspace(0.2*np.pi, 0.8*2 * np.pi, 100)
v = np.linspace(0.2, np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='g')


x = np.array([0*np.outer(np.cos(u[-1]), np.sin(v))[0],r*np.outer(np.cos(u[-1]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[-1]), np.sin(v))[0],r*np.outer(np.sin(u[-1]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[-1])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[-1])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='g')

x = np.array([0*np.outer(np.cos(u[0]), np.sin(v))[0],r*np.outer(np.cos(u[0]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[0]), np.sin(v))[0],r*np.outer(np.sin(u[0]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[0])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[0])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='g')







r=1

u = np.linspace(0.1*np.pi, 0.9*2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='yellow')


x = np.array([0*np.outer(np.cos(u[-1]), np.sin(v))[0],r*np.outer(np.cos(u[-1]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[-1]), np.sin(v))[0],r*np.outer(np.sin(u[-1]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[-1])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[-1])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='yellow')

x = np.array([0*np.outer(np.cos(u[0]), np.sin(v))[0],r*np.outer(np.cos(u[0]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[0]), np.sin(v))[0],r*np.outer(np.sin(u[0]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[0])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[0])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='yellow')

plt.draw()
plt.show()

我很累,但我认为matplotlib没有正确显示图层

这是从上面来的 enter image description here

这是从直接角度

enter image description here

任何帮助, 谢谢。

编辑2 我设法用matlab

做到了
figure()
hold on 
r=2
Rs = [1,2,3,4]
l=0.95
colors = ['g','r','b']

for i=1:length(Rs)
    l = l-0.015
    r = Rs(i)
    colr = colors(i)
    u = linspace(2*pi - l*2 * pi, l*2 * pi, 100);
    v = linspace(0,pi, 100);
    x = r * (cos(u)' * sin(v));
    y = r * (sin(u)' * sin(v));
    z = r * ones(length(u),1)* cos(v);
    surf(x,y,z,'faceColor',colr)%,'EdgeColor','none','LineStyle','none','FaceLighting','phong')
    hold on 


    x = [0*cos(u(end))'*sin(v); r* cos(u(end))'*sin(v)];
    y = [0*sin(u(end))'*sin(v); r*sin(u(end))'*sin(v)];
    z = [0*ones(length(u(end)),1)*cos(v);r*ones(length(u(end)),1)*cos(v)];
    surf(x,y,z,'faceColor',colr)%,'EdgeColor','none','LineStyle','none','FaceLighting','phong')


    x = [0*cos(u(1))'*sin(v); r* cos(u(1))'*sin(v)];
    y = [0*sin(u(1))'*sin(v); r*sin(u(1))'*sin(v)];
    z = [0*ones(length(u(1)),1)*cos(v);r*ones(length(u(1)),1)*cos(v)];
    surf(x,y,z,'faceColor',colr)%,'EdgeColor','none','LineStyle','none','FaceLighting','phong')
end
axis equal

1 个答案:

答案 0 :(得分:0)

这是在python中执行的方法(但是看起来不太好)

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




fig = plt.figure()



ax = fig.add_subplot(111, projection='3d')



r=2

u = np.linspace(0.2*np.pi, 0.8*2 * np.pi, 100)
v = np.linspace(0.2, np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='g')


x = np.array([0*np.outer(np.cos(u[-1]), np.sin(v))[0],r*np.outer(np.cos(u[-1]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[-1]), np.sin(v))[0],r*np.outer(np.sin(u[-1]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[-1])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[-1])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='g')

x = np.array([0*np.outer(np.cos(u[0]), np.sin(v))[0],r*np.outer(np.cos(u[0]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[0]), np.sin(v))[0],r*np.outer(np.sin(u[0]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[0])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[0])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='g')







r=1

u = np.linspace(0.1*np.pi, 0.9*2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='yellow')


x = np.array([0*np.outer(np.cos(u[-1]), np.sin(v))[0],r*np.outer(np.cos(u[-1]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[-1]), np.sin(v))[0],r*np.outer(np.sin(u[-1]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[-1])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[-1])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='yellow')

x = np.array([0*np.outer(np.cos(u[0]), np.sin(v))[0],r*np.outer(np.cos(u[0]), np.sin(v))[0]])
y = np.array([0*np.outer(np.sin(u[0]), np.sin(v))[0],r*np.outer(np.sin(u[0]), np.sin(v))[0]])
z = np.array([0* np.outer(np.ones(np.size(u[0])), np.cos(v))[0], r*np.outer(np.ones(np.size(u[0])), np.cos(v))[0]]) 
ax.plot_surface(x, y, z, rstride =5,
    cstride = 5, color ='yellow')

plt.draw()
plt.show()

这种在Matlab中的操作方式,效果很好。

figure()
hold on 
r=2
Rs = [1,2,3,4]
l=0.95
colors = ['g','r','b']

for i=1:length(Rs)
    l = l-0.015
    r = Rs(i)
    colr = colors(i)
    u = linspace(2*pi - l*2 * pi, l*2 * pi, 100);
    v = linspace(0,pi, 100);
    x = r * (cos(u)' * sin(v));
    y = r * (sin(u)' * sin(v));
    z = r * ones(length(u),1)* cos(v);
    surf(x,y,z,'faceColor',colr)%,'EdgeColor','none','LineStyle','none','FaceLighting','phong')
    hold on 


    x = [0*cos(u(end))'*sin(v); r* cos(u(end))'*sin(v)];
    y = [0*sin(u(end))'*sin(v); r*sin(u(end))'*sin(v)];
    z = [0*ones(length(u(end)),1)*cos(v);r*ones(length(u(end)),1)*cos(v)];
    surf(x,y,z,'faceColor',colr)%,'EdgeColor','none','LineStyle','none','FaceLighting','phong')


    x = [0*cos(u(1))'*sin(v); r* cos(u(1))'*sin(v)];
    y = [0*sin(u(1))'*sin(v); r*sin(u(1))'*sin(v)];
    z = [0*ones(length(u(1)),1)*cos(v);r*ones(length(u(1)),1)*cos(v)];
    surf(x,y,z,'faceColor',colr)%,'EdgeColor','none','LineStyle','none','FaceLighting','phong')
end
axis equal