ContourPlot picks a nice set of curves as level curves by default. I'm trying to use the set of curves as mesh curves for corresponding 3D plot, can anyone suggest how I can get those levels extracted out of contour plot?
f[x_, y_] := Log[(1 - x)^2 + 100 (y - x^2)^2];
ContourPlot[f[x, y], {x, -2, 0.5}, {y, -2, 2},
ContourStyle -> {{Gray, Thick}}, PlotPoints -> 100,
ColorFunction -> Function[GrayLevel[1 - .45 #]], Frame -> None]
Plot3D[f[x, y], {x, -2, 0.5}, {y, -2, 2}, PlotPoints -> 100,
MeshFunctions -> {#3 &}, Mesh -> {{0, 1, 2, 3, 4}}, Ticks -> None]
Answer
By default, contours generated by ContourPlot have Tooltips with their numerical value as the label. You can use Cases to grab the values programmatically.
f[x_, y_] := Log[(1 - x)^2 + 100 (y - x^2)^2];
plot = ContourPlot[f[x, y], {x, -2, 0.5}, {y, -2, 2},
ContourStyle -> {{Gray, Thick}}, PlotPoints -> 100,
ColorFunction -> Function[GrayLevel[1 - .45 #]], Frame -> None]
levels = Union@Cases[plot, Tooltip[_, label_] :> label, Infinity];
Plot3D[f[x, y], {x, -2, 0.5}, {y, -2, 2}, PlotPoints -> 100,
MeshFunctions -> {#3 &}, Mesh -> {levels}, Ticks -> None]


Comments
Post a Comment