Reputation: 147
I'm trying to simultaneously plot the contour lines and the surface of a function in Julia, but I can't seem to find a way to that.
Is there an easy way to achieve this?
Upvotes: 1
Views: 506
Reputation: 5700
Here is an example I worked out for Plots
. The PyPlot
package has something better, and something could be done with 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)
Upvotes: 2