I have the following problem while plotting with Plots.jl
. I like to plot the rosenbrock function
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
as surface, which expects a 2d Tuple{Float64,Float64}
as input.
What I could come up with, is the following:
using Plots
gr()
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
ts = linspace(-1.0, 1.0, 100)
x = ts
y = map(rosenbrock, [(x, z) for (x,z) in zip(ts,ts)])
z = map(rosenbrock, [(x, y) for (x,y) in zip(ts,ts)])
# plot(x, x, z)
plot(x, y, z, st = [:surface, :contourf])
I think I messed up some dimensions, but I don't see what I got wrong.
Do I have to nest the calculation of the mappings for y
and x
to get the result?
After a quick investigation of the Rosenbrock function I found, and correct me if Im wrong, but you need to specify the y
-vector you arent supposed to nest it within z
or anything like that
Someone else tried this same thing as shown here but using Plots
the solution is as follows as done by Patrick Kofod Mogensen
using Plots
function rosenbrock(x::Vector)
return (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
end
default(size=(600,600), fc=:heat)
x, y = -1.5:0.1:1.5, -1.5:0.1:1.5
z = Surface((x,y)->rosenbrock([x,y]), x, y)
surface(x,y,z, linealpha = 0.3)
This results in
side note
Im glad I searched for this as I've been searching for a 3D plotter for Julia other than PyPlot (as it can be a bit of a hassle to set up for the users of my program) and this even looks better and images can be rotated.