Plotting Ellipsoid with Matplotlib

前端 未结 2 618
遥遥无期
遥遥无期 2020-12-09 05:26

Does anyone have sample code for plotting ellipsoids? There is one for sphere on matplotlib site, but nothing for ellipsoids. I am trying to plot



        
2条回答
  •  时光取名叫无心
    2020-12-09 05:51

    Here is how you can do it via spherical coordinates:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure(figsize=plt.figaspect(1))  # Square figure
    ax = fig.add_subplot(111, projection='3d')
    
    coefs = (1, 2, 2)  # Coefficients in a0/c x**2 + a1/c y**2 + a2/c z**2 = 1 
    # Radii corresponding to the coefficients:
    rx, ry, rz = 1/np.sqrt(coefs)
    
    # Set of all spherical angles:
    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(0, np.pi, 100)
    
    # Cartesian coordinates that correspond to the spherical angles:
    # (this is the equation of an ellipsoid):
    x = rx * np.outer(np.cos(u), np.sin(v))
    y = ry * np.outer(np.sin(u), np.sin(v))
    z = rz * np.outer(np.ones_like(u), np.cos(v))
    
    # Plot:
    ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='b')
    
    # Adjustment of the axes, so that they all have the same span:
    max_radius = max(rx, ry, rz)
    for axis in 'xyz':
        getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
    
    plt.show()
    

    The resulting plot is similar to

    enter image description here

    The program above actually produces a nicer looking "square" graphics.

    This solution is strongly inspired from the example in Matplotlib's gallery.

提交回复
热议问题