matplotlib - Python plot of a piecewise defined surface -
i trying make 3d plot of surface defined in different ways different regions. example, take f(x,y) defined 1 if x > y , x^2 if x <= y.
i defined f logical operators, , tried plot "plot_surface" function, evaluating in grid. unfortunately, got error saying "the truth value of array more 1 element ambiguous".
do know way of solving this?
taking link posted serenity need define f using np.piecewise
import numpy np import matplotlib.pyplot plt mpl_toolkits.mplot3d import axes3d, axes3d num_steps = 500 x_arr = np.linspace(0,100, num_steps) y_arr = np.linspace(0,100, num_steps) def zfunc(x, y): return np.piecewise(x, [x>y, x<=y], [lambda x: 1, lambda x: x**2]) x,y = np.meshgrid(x_arr, y_arr) z =zfunc(x,y) fig=plt.figure() ax=fig.add_subplot(1,1,1,projection='3d') ax.set_xlabel('x axis') ax.set_ylabel('y axis') ax.plot_surface(x,y,z,cmap='viridis') #cmap make easier see ax.view_init(30, 70) plt.show()
Comments
Post a Comment