Plotting points on the surface of a sphere in Python's matplotlib

前端 未结 2 654
情话喂你
情话喂你 2021-01-30 14:09

I\'m trying to generate a plot of a sphere, with some points plotted on the surface of the sphere. (Specifically the points are the Lebedev quadrature points) I want my plot to

2条回答
  •  醉梦人生
    2021-01-30 15:11

    You can lower the alpha of the sphere if you think the points aren't showing up well enough. However, I think you may be processing the data into x, y, z coordinates incorrectly. I got a list of points from here: http://people.sc.fsu.edu/~jburkardt/m_src/sphere_lebedev_rule_display/sphere_lebedev_rule_display.html, and my sphere had points that looked kind of like yours until I realized that the file contained the values for theta and phi, and that I needed to turn degrees into radians.

    Here's the code I used:

    import matplotlib.pyplot as plt
    from matplotlib import cm, colors
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    
    # Create a sphere
    r = 1
    pi = np.pi
    cos = np.cos
    sin = np.sin
    phi, theta = np.mgrid[0.0:pi:100j, 0.0:2.0*pi:100j]
    x = r*sin(phi)*cos(theta)
    y = r*sin(phi)*sin(theta)
    z = r*cos(phi)
    
    #Import data
    data = np.genfromtxt('leb.txt')
    theta, phi, r = np.hsplit(data, 3) 
    theta = theta * pi / 180.0
    phi = phi * pi / 180.0
    xx = sin(phi)*cos(theta)
    yy = sin(phi)*sin(theta)
    zz = cos(phi)
    
    #Set colours and render
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    ax.plot_surface(
        x, y, z,  rstride=1, cstride=1, color='c', alpha=0.3, linewidth=0)
    
    ax.scatter(xx,yy,zz,color="k",s=20)
    
    ax.set_xlim([-1,1])
    ax.set_ylim([-1,1])
    ax.set_zlim([-1,1])
    ax.set_aspect("equal")
    plt.tight_layout()
    plt.show()
    

提交回复
热议问题