Search code examples
pythonsympymayavi

Sympy Lambdify get 3d plot for 2d equation in mayavi


  str_exp="x+y"
  expr = sympify((str_expr))
  X,Y,Z=np.ogrid[x_start:x_end:no_x_points , y_start:y_end:no_y_points , z_start:z_end:no_z_points]
  f = lambdify((x,y,z), expr)
  foo=f(X,Y,Z)

Even though expr is a function of x and y, I expect foo to produce 3d data as z is being varied too. When I try to plot foo using mayavi, a single line is being plotted instead of a plane.

I am sure that it is a consequence of sympy's lamdify as the dimension of foo is (100,100,1) when expr is x+y


Solution

  • I guess the problem would be that expr would have only 2 variables and hence you wouldn't get the 3d array as required. So, sympy's sympify function doesn't simplify the expression (or string given). So, you can add "(z+1)**2 - z*z - 2*z - 1" (or some simple expression such that the z term doesn't cancel out) to str_exp if you don't have 'z' term in it.

    So this way sympify would return you an expression with z.

    >>str_expr="x+y+(z+1)**2 - z*z - 2*z - 1"
    >>sympify((str_expr))
    x + y - z**2 - 2*z + (z + 1)**2 - 1