Plotting implicit equations in 3d

前端 未结 8 673
遇见更好的自我
遇见更好的自我 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 07:59

    Finally, I did it (I updated my matplotlib to 1.0.1). Here is code:

    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    
    def hyp_part1(x,y,z):
        return -(x**2) - (y**2) + (z**2) - 1
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    x_range = np.arange(-100,100,10) 
    y_range = np.arange(-100,100,10)
    X,Y = np.meshgrid(x_range,y_range)
    A = np.linspace(-100, 100, 15)
    
    A1,A2 = np.meshgrid(A,A)    
    
    for z in A: 
        X,Y = A1, A2
        Z = hyp_part1(X,Y,z)
        ax.contour(X, Y, Z+z, [z], zdir='z')
    
    for y in A: 
        X,Z= A1, A2
        Y = hyp_part1(X,y,Z)
        ax.contour(X, Y+y, Z, [y], zdir='y')
    
    for x in A:
        Y,Z = A1, A2 
        X = hyp_part1(x,Y,Z)
        ax.contour(X+x, Y, Z, [x], zdir='x')
    
    ax.set_zlim3d(-100,100)
    ax.set_xlim3d(-100,100)
    ax.set_ylim3d(-100,100)
    

    Here is result:alt text

    Thank You, Paul!

提交回复
热议问题