# Adding z-value to mesh lines in Plot3D as is done in ContourPlot

Consider:

f[x_, y_] = (x^3 + y^3)/(x^2 + y^2);
Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1},
MeshFunctions -> {#3 &}]


Which produces this image:

Is there a way to reveal the z-value of each mesh line by hovering your mouse over the mesh line, similar to what is done automatically in ContourPlot?

• Probably better to just display the 3D plot and contour plot side-to-side, with the contour plot acting as a "dictionary". Jul 15, 2015 at 5:12

Since V8.0.4 (or earlier?), we have Tooltip for 3D graphics. Here's one way via post-processing.

f[x_, y_] = (x^3 + y^3)/(x^2 + y^2);
plot = Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1}, MeshFunctions -> {#3 &}]

plot /. Line[pp_] :> Tooltip[Line[pp], plot[[1, 1, First[pp], 3]]]


If you make the mesh lines thicker, they'll be a little easier to hover over.

First alternative

Normal[plot] /. Line[pp_] :> Tooltip[Line[pp], pp[[1, 3]]]


Second alternative

Normal@Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1},
MeshFunctions -> {#3 &}, MeshStyle -> {Tooltip[#] &}] /.
Tooltip[l_Line] :> Tooltip[l, l[[1, 1, 3]]]


Notes: (1) The original answer is most efficient (since it retains the GraphicsComplex), but it relies on the internal structure of the graphics. (2) Purists may object to Tooltip[#] & in the second alternative, but they should try just Tooltip; it fails for me. I cannot conceive of a way to use the MeshStyle option without post-processing.

• Nice to know :) +1 Jul 15, 2015 at 5:19
• (+1) This works in version 8.0.4 under Win7 x64. Jul 15, 2015 at 9:47
• @MichaelE2 Great answer. However, I am still very much a rookie at plot /. Line[pp_] :> Tooltip[Line[pp], plot[[1, 1, First[pp], 3]]]. I looked up Tooltip and it says Tooltip[expr,label] displays the label when mouse is hovered over expr. OK, I think what is stored in pp is a list of indices for a particular Line command. I tried plot[[1,1]] and I saw a list of all points used in the plot, so First[pp] should take the first index use by the line command (might be 506), which means it selects the 506 point in the point list, then the 3 takes the z-value of that point (x,y,z). Am I OK? Jul 15, 2015 at 17:47
• @David Yep, that's exactly it. I've got another alternative for you.... Jul 15, 2015 at 19:15