Plotting implicit equations in 3d

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

    As far as I know, it is not possible. You have to solve this equation numerically by yourself. Using scipy.optimize is a good idea. The simplest case is that you know the range of the surface that you want to plot, and just make a regular grid in x and y, and try to solve equation F(xi,yi,z)=0 for z, giving a starting point of z. Following is a very dirty code that might help you

    from scipy import *
    from scipy import optimize
    
    xrange = (0,1)
    yrange = (0,1)
    density = 100
    startz = 1
    
    def F(x,y,z):
        return x**2+y**2+z**2-10
    
    x = linspace(xrange[0],xrange[1],density)
    y = linspace(yrange[0],yrange[1],density)
    
    points = []
    for xi in x:
        for yi in y:
            g = lambda z:F(xi,yi,z)
            res = optimize.fsolve(g, startz, full_output=1)
            if res[2] == 1:
                zi = res[0]
                points.append([xi,yi,zi])
    
    points = array(points)
    

提交回复
热议问题