同时等高线和 3d 图
Simultaneous Contour and 3d Plot
我试图在 Julia 中同时绘制函数的等高线和曲面,但我似乎找不到方法。
有没有简单的方法可以做到这一点?
这是我为 Plots
设计的一个例子。 PyPlot
包有更好的东西,可以用 Makie 做一些事情:
import Contour: contours, levels, level, lines, coordinates
function surface_contour(xs, ys, f; offset=0)
p = surface(xs, ys, f, legend=false, fillalpha=0.5)
## we add to the graphic p, then plot
zs = [f(x,y) for x in xs, y in ys] # reverse order for use with Contour package
for cl in levels(contours(xs, ys, zs))
lvl = level(cl) # the z-value of this contour level
for line in lines(cl)
_xs, _ys = coordinates(line) # coordinates of this line segment
_zs = offset .+ 0 .* _xs
plot!(p, _xs, _ys, _zs, alpha=0.5) # add curve on x-y plane
end
end
p
end
xs = ys = range(-pi, stop=pi, length=100)
f(x,y) = 2 + sin(x) - cos(y)
surface_contour(xs, ys, f)
我试图在 Julia 中同时绘制函数的等高线和曲面,但我似乎找不到方法。
有没有简单的方法可以做到这一点?
这是我为 Plots
设计的一个例子。 PyPlot
包有更好的东西,可以用 Makie 做一些事情:
import Contour: contours, levels, level, lines, coordinates
function surface_contour(xs, ys, f; offset=0)
p = surface(xs, ys, f, legend=false, fillalpha=0.5)
## we add to the graphic p, then plot
zs = [f(x,y) for x in xs, y in ys] # reverse order for use with Contour package
for cl in levels(contours(xs, ys, zs))
lvl = level(cl) # the z-value of this contour level
for line in lines(cl)
_xs, _ys = coordinates(line) # coordinates of this line segment
_zs = offset .+ 0 .* _xs
plot!(p, _xs, _ys, _zs, alpha=0.5) # add curve on x-y plane
end
end
p
end
xs = ys = range(-pi, stop=pi, length=100)
f(x,y) = 2 + sin(x) - cos(y)
surface_contour(xs, ys, f)