使用 `Plots.jl` 在 3d space 中绘制 2d 函数作为表面
plotting a 2d function as surface in 3d space with `Plots.jl`
我在使用 Plots.jl
绘图时遇到以下问题。我喜欢绘制 rosenbrock 函数
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
作为表面,需要 2d Tuple{Float64,Float64}
作为输入。
我能想到的是:
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])
产生这个图:
我想我弄乱了一些尺寸,但我看不出我错了什么。
是否必须嵌套 y
和 x
的映射计算才能得到结果?
在快速调查我发现的 Rosenbrock 函数后,如果我错了请纠正我,但你需要指定 y
-vector 你不应该将它嵌套在 z
或任何东西中像那样
其他人尝试了与 here 所示相同的操作,但使用的是 Plots
解决方法如下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)
这导致
旁注
我很高兴我搜索了这个,因为我一直在寻找 PyPlot 以外的 Julia 3D 绘图仪(因为为我的程序的用户设置它可能有点麻烦),这甚至看起来更好,图像可以旋转。
我在使用 Plots.jl
绘图时遇到以下问题。我喜欢绘制 rosenbrock 函数
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
作为表面,需要 2d Tuple{Float64,Float64}
作为输入。
我能想到的是:
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])
产生这个图:
我想我弄乱了一些尺寸,但我看不出我错了什么。
是否必须嵌套 y
和 x
的映射计算才能得到结果?
在快速调查我发现的 Rosenbrock 函数后,如果我错了请纠正我,但你需要指定 y
-vector 你不应该将它嵌套在 z
或任何东西中像那样
其他人尝试了与 here 所示相同的操作,但使用的是 Plots
解决方法如下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)
这导致
旁注
我很高兴我搜索了这个,因为我一直在寻找 PyPlot 以外的 Julia 3D 绘图仪(因为为我的程序的用户设置它可能有点麻烦),这甚至看起来更好,图像可以旋转。