4
$\begingroup$

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:

enter image description here

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?

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

1 Answer 1

8
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Nice to know :) +1 $\endgroup$ Jul 15, 2015 at 5:19
  • 1
    $\begingroup$ (+1) This works in version 8.0.4 under Win7 x64. $\endgroup$ Jul 15, 2015 at 9:47
  • $\begingroup$ @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? $\endgroup$
    – David
    Jul 15, 2015 at 17:47
  • $\begingroup$ @David Yep, that's exactly it. I've got another alternative for you.... $\endgroup$
    – Michael E2
    Jul 15, 2015 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.