Plotting implicit equations in 3d

前端 未结 8 624
遇见更好的自我
遇见更好的自我 2020-12-02 07:04

I\'d like to plot implicit equation F(x,y,z) = 0 in 3D. Is it possible in Matplotlib?

相关标签:
8条回答
  • 2020-12-02 08:02

    Actually there is an easy way to plot implicit 3D surface with the scikit-image package. The key is the marching_cubes method.

    import numpy as np
    from skimage import measure
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import axes3d
    

    Then we compute the function over a 3D meshgrid, in this example we use the goursat_tangle method @Paul defined in its answer:

    xl = np.linspace(-3, 3, 50)
    X, Y, Z = np.meshgrid(xl, xl, xl)
    F = goursat_tangle(X, Y, Z)
    

    The magic is happening here with marching_cubes:

    verts, faces, normals, values = measure.marching_cubes(F, 0, spacing=[np.diff(xl)[0]]*3)
    verts -= 3
    

    We just need to correct vertices coordinates as they are expressed in Voxel coordinates (hence scaling using spacing switch and the subsequent origin shift).

    Finally it is just about rendering the iso-surface using tri_surface:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2], cmap='jet', lw=0)
    

    Which returns:

    0 讨论(0)
  • 2020-12-02 08:07

    Have you looked at mplot3d on matplotlib?

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