How to join these two 3D lines together with a surface in Python's matplotlib

后端 未结 1 1667
灰色年华
灰色年华 2021-01-06 19:52

I have two orbits which occur at different heights. I plot them in 3D, but I wish to join them together with a surface. So far I have this picture:

which I get usi

相关标签:
1条回答
  • 2021-01-06 20:01

    You'll want to find the equation for the line connecting the ith point in the first orbit to the ith point in the second orbit. Then you can use i and z as parameters, varying over all the possible values to find X and Y.

    Example:

    import numpy as np
    import matplotlib
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    Z1 = 8.0
    Z2 = 9.0
    font = {'size'   : 18}
    matplotlib.rc('font', **font)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    t = np.linspace(0, 2 * np.pi, 100)
    x = np.cos(t)
    y = np.sin(2 * t)
    N = len(x)
    z = np.zeros(N)
    z[:,] = Z1
    
    t = np.linspace(0, 2 * np.pi, 100)
    x2 = 2 * np.cos(t)
    y2 = 2 * np.sin(2*t)
    N = len(x2)
    z2 = np.zeros(N)
    z2[:,] = Z2 
    
    #Plot the first orbit
    ax.plot(x, y, z, 'k-', linewidth=3.0)
    #Plot second orbit
    ax.plot(x2, y2, z2, 'k-', linewidth=3.0)
    
    
    i, h = np.meshgrid(np.arange(len(x)), np.linspace(Z1, Z2, 10))
    X = (x2[i] - x[i]) / (Z2 - Z1) * (h - Z1) + x[i]
    Y = (y2[i] - y[i]) / (Z2 - Z1) * (h - Z1) + y[i]
    surf = ax.plot_surface(X, Y, h, color='m', alpha=0.3,
             linewidth=0)
    
    
    #Set axis and things
    ax.set_xticks([1.0,1.5,2])
    ax.set_yticks([32,35,38])
    ax.set_ylabel('$||u||_{2}$', fontsize=26, rotation=0, labelpad = 26)
    ax.set_xlabel('$h$', fontsize=26)
    ax.set_zlabel('$\mu$', fontsize=26, rotation=90)
    plt.tight_layout()
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题