使用python,我希望绘制一个给定半径为r的3D球形帽

时间:2015-04-11 18:15:01

标签: python matplotlib plot 3d geometry

我正在制作一个小程序,它将生成spherical cap给定参数h和a的3D图。

非常感谢任何帮助!

我已经开始使用matplotlib示例绘制球体......

我想为a和h输入一个值,得到相应的球体半径,然后绘制一个高度h和基本半径a的球冠。理想情况下,3d图上的z轴和x y轴将对应于我输入的初始a和h(只要它具有我推测的几何意义?)

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

def capRatio(r, a, h):
    '''cap to sphere ratio'''
    surface_cap = np.pi * (a**2 + h**2)
    surface_sphere = 4.0 * np.pi * r**2
    return surface_cap/surface_sphere

def findRadius(a, h):
    "find radius if you have cap base radius a and height"
    r = (a**2 + h**2) / (2*h)
    return r

#choose a and h
a = 4
h = 3
r = findRadius(a,h)
p = capRatio(r, a, h) # Ratio of sphere to be plotted, could also be a function of a.
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, p*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)) 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=4, cstride=4, alpha=0.3 , cmap= cm.winter)
plt.show()

1 个答案:

答案 0 :(得分:0)

下一部分是限制你接受的坐标范围,这可以通过几种方式完成。以下是一种似乎有效的方法:

p = 0.8 # Ratio of sphere to be plotted, could also be a function of a.

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, p*np.pi, p*100)