Reputation: 21
I'm trying to replicate a python plot from Maple. I want the plot to be limited inside the wall of the cube. It's supposed to look clean like this: Maple Plot
This is the codes from Maple:
> Digits := 20;with(plots);s := 2;
> x := array(1 .. 4);y := array(1 .. 4);for i to 4 do x[i] := evalf(-1 + 2*rand()/10^12); y[i] := evalf(-1 + 2*rand()/10^12); end do;
> F := (u, v) -> ((u - x[1])^2 + (v - y[1])^2)^(-1/2*s) + ((u - x[2])^2 + (v - y[2])^2)^(-1/2*s) + ((u - x[3])^2 + (v - y[3])^2)^(-1/2*s) + ((u - x[4])^2 + (v - y[4])^2)^(-1/2*s);
> Minimize(F(u, v), u = -1 .. 1, v = -1 .. 1);
[3.0238548714143095703, [u = 1.0, v = 1.0]]
> plot3d(F(u, v), u = -1 .. 1, v = -1 .. 1, view = 0 .. 5*F(1, 1));
This is my python code and the plot output. Please help!
def f(u ,v):
z = ((u - x[0]) ** 2 + (v - y[0]) ** 2) ** (-s * 0.5) \
+ ((u - x[1]) ** 2 + (v - y[1]) ** 2) ** (-s * 0.5) \
+ ((u - x[2]) ** 2 + (v - y[2]) ** 2) ** (-s * 0.5) \
+ ((u - x[3]) ** 2 + (v - y[3]) ** 2) ** (-s * 0.5)
return z
U = np.linspace(-1, 1)
V = np.linspace(-1, 1)
X, Y = np.meshgrid(U, V)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')
ax.set_zlim(0, 5*f(1,1))
ax.set_title('Surface plot')
ax.set_xlabel('u')
ax.set_ylabel('v')
plt.show()
The plot should stay inside the axis and should be cut where I put the limit (something similar to Maple's plot). But it instead "flows" outside and has some strange vertical lines. Python Plot
Upvotes: 2
Views: 132
Reputation: 56
In Python the trick is to not show values above a certain threshold. Therefore, before plotting you need
z_lim = 5 * F(1,1)
masked_z = np.ma.masked_where(Z > z_lim, Z)
so you then plot
ax.plot_surface(X, Y, masked_z, cmap='viridis', edgecolor='none')
Upvotes: 0