Plotting a 3D Surface


#1

How would I plot the surface f(x,y)=xy-y-x+1 between \sqrt2\leq y\leq2

using PyPlot;
x = collect(Float16, range(-2,length=100,stop=2));
y = collect(Float16, range(sqrt(2),length=100, stop=2));
z = (x.*y).-y.-x.+1;
surf(x,y,z);

Figure_1-1


#2

It is not quite clear to me what the question is. However, you can use Plots as follows:

using Plots; pyplot()
x=range(-2,stop=2,length=100)
y=range(sqrt(2),stop=2,length=100)
f(x,y) = x*y-x-y+1
plot(x,y,f,st=:surface,camera=(-30,30))

with result:
image
Here, st is short hand for seriestype (or something similar). Keyword camera sets the camera/viewing angles. I have used the default heatmap.
If you want to plot contours, you can plot contour lines (st=:contour) or filled countours (st=:contour).


#3

…if you want to use another heatmap, just set the color (or c) argument to some color gradient, e.g., a built in such as :blues or make your own, say:

my_cg = cgrad([:red,:blue])

thus

plot(x,y,f,st=:surface,c=my_cg,camera=(-30,30))

leading to:
image


#4

I think using PyPlot.surf was misguided since I had tried.

using PyPlot;
x = collect(Float16, range(-2,length=100,stop=2));
y = collect(Float16, range(sqrt(2),length=100, stop=2));
f(x,y) = x*y-x-y+1
surf(x,y,f);
ERROR: MethodError: no method matching 
surf(::Array{Float16,1}, ::Array{Float16,1}, ::typeof(f))

Clearly PyPlot \neq {Plots,pyplot()}


#5

Well, it is possible that Plots with pyplot() sets some defaults for PyPlot, so that the Plots results are uniform for different backends such as pyplot(), gr(), etc. In any way, I’ve changed the camera angle — I think you often need to do that to get ok plots with 3D surface plots. Here is the default camera angle, with c = :blues:
image


#6

I do notice that my way of plotting lets f(x,y) range from ca. -3 and upwards, while your plot seems to range from -1. Perhaps that is why it looks differently? I’ve checked the function plot in WolframAlpha, and the result I have looks very similar to that plot. I haven’t tried to use PyPlot directly from Julia.