Revision history [back]

Instead of plot3d, you could use implicit_plot3d. This allows a better control of the min and max values of the z-coordinate. Please, check the following code:

var("x,y,z")
h(x,y)= y/(x^2+y^2)

cm = colormaps.Spectral
zmin, zmax = -2, 2
def c(x,y,z):
return float((h(x,y)-zmin)/(zmax-zmin))

S = implicit_plot3d(z==h, (x,-1,1), (y,-1,1), (z,zmin,zmax),
color = (cm,c), region=lambda x,y,z: x^2+y^2+z^2>0.001)
show(S, aspect_ratio=[2,2,1], viewer="threejs")

C=contour_plot(h, (x,-1.5,1.5), (y,-1.5,1.5), cmap ="Spectral",
contours = [-2,-1,-0.5,-0.25,0,0.25,0.5,1,2],
colorbar = True, axes = True,
labels = True, label_colors="black",
label_inline=True, label_fontsize=8,
gridlines=True, axes_labels=["$x$","$y$"])
show(C, figsize=8)


You can see the result in this SageCell

Observe that now the color function needs three arguments. It always takes values between $0$ and $1$, since the $z$ range is constrained to the interval [zmin,zmax]. Likewise, since the contours also vary between zmin=-2 and zmax=2, colors in the surface match those in the contour plot. Points not satisfying the condition given by the region key are excluded from the plot, which, in this case, are points lying in a small ball centered at the origin. This allows to avoid the singularity of the function $h$. Hope that helps.