Message Boards Message Boards


Attach a label to a manipulatable point

Posted 2 months ago
3 Replies
1 Total Likes

Can I attach a label to the red point in the attached notebook showing its coordinates without creating a third graphics image?

Context: I want to write a manipulate[] function (f2) that takes a real-value function (f1) of one variable and a domain interval as arguments, then puts a manipulatable point on f1's plot with a label containing the point's coordinates. And I'd like f2 to work with continuous and piecewise functions like the example f1 attached.

Alternate question: Has someone already done this? It seems like something other folks would want to do.

3 Replies


I generally believe that Show is not a good approach for this. I would use Epilog to add annotations to plots.

 Plot[myIndicatorFunc[-2, 5, x], {x, -10, 10}, 
  Epilog -> {Directive[PointSize[0.02], Red], 
    Point[{y, myIndicatorFunc[-2, 5, y]}], Directive[Blue], 
    Text["point: " <> ToString[{y, myIndicatorFunc[-2, 5, y]}], {y, 
      myIndicatorFunc[-2, 5, y] + .05}]}, 
  PlotRangePadding -> Scaled[0.1]], {y, -10, 10, 0.5, 
  AnimationRate -> 5}]

I used Epilog with Directives to set the style for the point and the text. I used PlotRangePadding to give extra room for the label as it moves around. I changed the animation from 1/2 to 0.5 because Mathematica tries to use fractions which do not turn into text very well (i.e. 5/2 vs 2.5). With Epilog you can pass a bunch of plot annotations.

I hope this helps.



That's perfect, Neil. Thanks. As you know, I'm new here. I'll study up on Epilog. I assume MMA means Mathematica. Please let me know if that's wrong.

It is disappointing that the new FunctionDiscontinuities does not seem to collaborate with the old Exclusions -> "Discontinuities":

myIndicatorFunc[a_, b_, var_] := 
 Piecewise[{{0, var < a}, {1/2, var == a}, {1, a < var < b}, {1/2, 
    var == b}, {0, b < var}}]
FunctionDiscontinuities[myIndicatorFunc[-2, 5, y], y]
Plot[myIndicatorFunc[-2, 5, x], {x, -10, 10}, 
 Exclusions -> "Discontinuities"]
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract