Plotting in Julia like Matlab plots

I’m trying to plot 3D surface plot like the figure below (source: Wikipedia):

512px-Rosenbrock_function.svg

Here is my attempt in julia;

x  = [-2:0.05:2;]
y = [-1:0.05:3;]
z = (1 .-x').^2 .+ 100 .*(y.-x'.^2).^2

minZ = minimum(z[:]);  
maxZ = maximum(z[:]);

c =  minZ .+ (maxZ-minZ).*log.(1 .+z .- minZ) ./ log(1+maxZ-minZ)
Plots.plot(x,y,z,st=:surface,color=cgrad(:jet,c),
xlabel = "x",ylabel="y",zlabel="f(x,y)")

rosenbrock

Here are my questions:

  1. How can I get the julia plot color map appear like Matlab, even log scale does not work.
  2. View (camera) for Matlab plot Azimuth and Elevation is (-30,30) while julia plot camera option does not work at all.camera = (-30,30). Negative values does not seem to work in camera option. How can me make the view (x and y option) appear similar to matlab.

Thank you

This recent post may be useful:

Do you use the gr backend? Seems like camera doesn’t work with that backend.

I did some hacking, and get this (imperfect) result:

Some intro things:

using Plots; pyplot()
COL = append!([colorant"blue",colorant"lime"],range(colorant"yellow",colorant"red",length=20))

gives:

You can play around to get a better vector of colors. Next, I apply these colors to the cgrad method, and add scale=:exp:

Plots.plot(x,y,z,st=:surface,color=cgrad(COL,scale=:exp),#cgrad(:jet,c),
xlabel = "x",ylabel="y",zlabel="f(x,y)",camera=(-30,30))

This gives the introductory plot.

See, e.g., https://www.w3schools.com/colors/colors_names.asp for names of HTML colors – I think most of them are supported in Plots – you just write the HTML name in lowercase.

Also, see http://docs.juliaplots.org/latest/generated/colorschemes/ for predefined color gradients.

1 Like

You may get reasonably close with the following code (using Gnuplot.jl):

using Gnuplot

x  = [-2:0.05:2;]
y = [-1:0.05:3;]
z = (1 .-x').^2 .+ 100 .*(y.-x'.^2).^2

minZ = minimum(z[:]);  
maxZ = maximum(z[:]);

c =  minZ .+ (maxZ-minZ).*log.(1 .+z .- minZ) ./ log(1+maxZ-minZ)

@gsp "set xyplane at 0" palette(:jet1) cbr=extrema(c) :-
@gsp :- x y z c "w pm3d notit"

@gsp :- xlab="x" ylab="y" "set zlabel 'f(x,y)' rot parallel" :-
@gsp :- xr=[-2.01,NaN] yr=[-1.01, NaN]
@gsp :- "set view 50, 240"
@gsp :- "set grid vertical"
@gsp :- "set grid ztics"

and save the image with:

save(output="test.png", term="pngcairo size 800,600")

The result is:

(Edit: added grid lines)

3 Likes

This is excellent. I’m very familiar with gnuplot, thanks for sharing the package and the code. One question, can I save the plot in Julia environment, and further use to add features? Lets say for example, I would like to add 3D quiver plot using Julia commands on top of the surface plot, is that possible, or should I still do this in gnuplot?

Awesome, thank you. Is there a way to rotate “f(x,y)” zlabel and move away from the axis? Thanks again.

Despite all the efforts, I think the Matlab version is still most beautiful :heart_eyes:

I’d love to see any of our more artistically inclined data viz experts try to improve the Julia version :blush:

2 Likes

Something like this?

Plots.plot(x,y,z,st=:surface,color=cgrad(COL,scale=:exp),#cgrad(:jet,c),
xlabel = "x",ylabel="y",zlabel="f(x,y)",zguidefontrotation=45,camera=(-30,30))

leads to:

2 Likes

Perfect, yes.