绘制球形帽

时间:2017-07-27 07:44:30

标签: python plot 3d mayavi

使用Python,我想用一种颜色绘制球体,并在该球体上绘制一些spherical caps。上限由其中心点(角度phitheta)和半径给出。

enter image description here

这是否可能,可能是MayaVi

1 个答案:

答案 0 :(得分:0)

使用matplotlib:

def plot_spherical_cap(ax, b, opening_angle, radius=1.0):
    r = elevation
    phi = numpy.linspace(0, 2 * numpy.pi, 30)
    theta = numpy.linspace(0, opening_angle, 20)
    X = r * numpy.stack([
        numpy.outer(numpy.cos(phi), numpy.sin(theta)),
        numpy.outer(numpy.sin(phi), numpy.sin(theta)),
        numpy.outer(numpy.ones(numpy.size(phi)), numpy.cos(theta)),
        ], axis=-1)

    # rotate X such that [0, 0, 1] gets rotated to `c`;
    # <https://math.stackexchange.com/a/476311/36678>.
    a = numpy.array([0.0, 0.0, 1.0])
    a_x_b = numpy.cross(a, b)
    a_dot_b = numpy.dot(a, b)
    if a_dot_b == -1.0:
        X_rot = -X
    else:
        X_rot = (
            X +
            numpy.cross(a_x_b, X) +
            numpy.cross(a_x_b, numpy.cross(a_x_b, X)) / (1.0 + a_dot_b)
            )

    ax.plot_surface(
            X_rot[..., 0], X_rot[..., 1], X_rot[..., 2],
            rstride=3, cstride=3,
            color='#1f77b4',
            alpha=0.5,
            linewidth=0
            )
    return

enter image description here