I am trying to plot the surface : W = (X*sin(Y)+Z)^2
Is there a way to do it in Python?
In general tests I did on Google I did not find anything relevant.
I would appreciate your help.
I am trying to plot the surface : W = (X*sin(Y)+Z)^2
Is there a way to do it in Python?
In general tests I did on Google I did not find anything relevant.
I would appreciate your help.
You can try to see your function W(X,Y,Z)
using video of slices.
For example, to see slices along the z
-axis:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = fig.gca(projection='3d')
W = lambda X, Y, Z: (X*np.sin(Y)+Z)**2
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
z = np.linspace(0, 10, 100)
ax.set_xlim3d(min(x), max(x))
ax.set_ylim3d(min(y), max(y))
ax.set_zlim3d(min(z), max(z))
X, Y = np.meshgrid(x, y)
def animate_along_z(i):
z0 = z[i]
Z = W(X, Y, z0)
levels = np.linspace(Z.min(), Z.max(), 40)
ax.contourf(X, Y, Z, zdir='z', levels=levels, offset=z0, alpha=1)
ani = FuncAnimation(fig, animate_along_z, interval=len(z))
plt.show()
Check also plotly
it looks like MATLAB slice
.
Another solution is to plot an iso-surface (or multiple iso-surfaces) at a fixed value of W
.
Scatter3D plot
# Creating dataset
x,y,z = np.meshgrid(np.linspace(0, 10, 20),
np.linspace(0, 10, 20),
np.linspace(0, 10, 20))
w = W(x,y,z)
# Creating plot
ax.scatter3D(x, y, z, alpha = 0.8, c = w, cmap = plt.get_cmap('jet'), marker = '.')
plt.show()
Scatter path
# Creating parametric line
t = np.linspace(0, 10, 100)
x = 10*np.sin(t)
y = 10*np.cos(t)
z = t
w = W(x,y,z)
# Creating plot
ax.scatter3D(x, y, z, alpha = 0.8, c = w, cmap = plt.get_cmap('hsv'), marker = '.')
plt.show()
It depends on what you want to see. If you want to see samples of the function, you could go for a parallel coordinates plot. You could also fix x,y or z, so you get a 3-dimensional surface. You definitely need some interaction or constraint to plot this function, since it has 3 degrees-of-freedom.