2
$\begingroup$

How do I label a point that I add to a plot of some kind using Show[plot,Graphics[...]], from the documentation I cannot find out the correct way. I would have thought that the following would work but it produces an error:

Show[RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}], Labeled[Graphics[{Red, Point[{1/6, 1/3}]}] , "a"] ] 

(See also this closed question: Placing a label at a point, which was judged off-topic even though it looks completely on-topic too me.)

$\endgroup$
2
  • $\begingroup$ Show[RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}], Graphics[{Red, Point[{1/6, 1/3}], Black, Text["a", {1/6 - .1, 1/3}]}]] or RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}, Epilog -> {Red, Point[{1/6, 1/3}], Black, Text["a", {1/6 - .1, 1/3}]}] $\endgroup$ Commented Nov 5, 2020 at 16:59
  • $\begingroup$ @RohitNamjoshi, thanks! I think I consider that a full answer so you could put it as an answer if you want to. $\endgroup$ Commented Nov 5, 2020 at 17:10

1 Answer 1

4
$\begingroup$

It does not work because Labeled is not a Graphics primitive or directive. Some alternatives

Using the Text primitive and Show

Show[ RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}], Graphics[{Red, Point[{1/6, 1/3}], Black, Text["a", {1/6 - .1, 1/3}]}]] 

enter image description here

Using Epilog

RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}, Epilog -> {Red, Point[{1/6, 1/3}], Black, Text["a", {1/6 - .1, 1/3}]}] 

The result is the same as using Show.

Using ListPlot, Callout and Show

Show[RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}], ListPlot[Callout[{{1/6, 1/3}}, "a", Left], Axes -> False, PlotStyle -> Red]] 

enter image description here

With Callout there are many options for customizing the appearance.

$\endgroup$
6
  • $\begingroup$ In the first case, how is the position determined. Does {1/6 - .1, 1/3} give the middle or the left most point of the text and can this be changed? (I think it is the middle which makes position more difficult.) $\endgroup$ Commented Apr 7, 2023 at 12:37
  • $\begingroup$ @Kvothe I don't really understand your question. The coordinate of the point is 1/6, 1/3}. In the code, the x position is reduced by 0.1 to ensure that the text does not overlap the point. If you want to adjust the offset based on the length/size/font of the text then you need to compute its bounding box. There are ways to do that e.g. this. $\endgroup$ Commented Apr 7, 2023 at 19:14
  • $\begingroup$ I was asking what the provided position {1/6 - .1, 1/3} is used for and whether it can be changed. Logical possibilities would be the left beginning middle or end of the text. A simple test, i.e. just replace "a" with a longer text, shows that the provided position is used to set the middle of the text. I was wondering whether this can be changed. For example in your example it would be convenient if we could set the position of the end of the word. That way if we increased the length of the label it would still be positioned nicely and not intersect with the point and the line. $\endgroup$ Commented Apr 8, 2023 at 9:48
  • 1
    $\begingroup$ @Kvothe Ahh, yes there is a way. Text takes a position and optional offset. Check the documentation. E.g. Show[RegionPlot[x > y, {x, -2, 2}, {y, -2, 2}], Graphics[{Red, Point[{1/6, 1/3}], Black, Text["abcdefghijklmnopqr", {1/6 - .1, 1/3}, {1, 0}]}]] $\endgroup$ Commented Apr 9, 2023 at 3:04
  • $\begingroup$ Thanks! (And sorry for accepting so late I must have forgotten last time. I didn't even notice it was my own question this time haha.) $\endgroup$ Commented Apr 10, 2023 at 18:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.