0

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.

had
  • 49
  • 3
  • [This](https://stackoverflow.com/a/40452621/8881141) and [this](https://stackoverflow.com/a/32480062/8881141) should provide the answer you are looking for. – Mr. T Mar 05 '21 at 11:08

2 Answers2

2

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()

enter image description here

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()

enter image description here


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()

enter image description here

Mariya
  • 46
  • 4
  • Amazing! Thank you, you helped me a lot! – had Mar 05 '21 at 13:09
  • 1
    It would be my pleasure, if it suits you, you can accept my answer. – Mariya Mar 05 '21 at 18:34
  • Is there a way to do scatter on it ? – had Mar 07 '21 at 20:17
  • Yes, you can do 3D scatter for a regular mesh (see example in the answer) or for a random mesh. – Mariya Mar 07 '21 at 21:20
  • I'm trying to plot some path. I have 4 arrays and I want to show them on the graph. I came up with something like this: ax.scatter3D(a_arr, b_arr, c_arr, alpha = 0.8, c = error_arr, cmap = plt.get_cmap('hsv'), marker = '.') Thats the way to do it ? – had Mar 07 '21 at 23:12
  • No problem, you can draw the path (see my corrected answer). – Mariya Mar 12 '21 at 07:28
0

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.

Gabend
  • 91
  • 4
  • I want to plot the surface as three-dimensional, and the fourth dimension to represent using color. Do you have an example of something like that? – had Mar 05 '21 at 11:12