Skip to content Skip to sidebar Skip to footer

Python Plot Of A Piecewise Defined Surface

I am trying to make a 3d plot of a surface that is defined in different ways for different regions. As an example, take f(x,y) that is defined as 1 if x > y and as x^2 if x <

Solution 1:

Taking from the link posted by Serenity you need to define f using np.piecewise

import numpy as np
import matplotlib.pyplot as plt
from 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)

defzfunc(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 to make it easier to see 
ax.view_init(30, 70)
plt.show()

Giving you this plot: enter image description here

Post a Comment for "Python Plot Of A Piecewise Defined Surface"