I have some random graphics with specified ImageSize and AspectRatio, e.g., this line

g1 = Graphics[Line[{{1, 0}, {2, 1}, {3, 0}, {4, 1}}], ImageSize -> 230, AspectRatio -> 1/2]

and some text

g2 = Text[Style["1+1", Red, Large, Bold], Background -> LightRed]

as shown below:

enter image description here

How do I "overlay" the text g2 ontop of g1 and specify the relative position? For example, I want the center of g2 to be located at {1/3,7/8} of g1, which means 1/3 the width and 7/8 of the height of g1, both measured from the lower left corner of g1. By doing this, the text g2 will be just below the first peak of g1. How can this be done?


1 Answer 1


You can put text directly into Graphicsthus

   Graphics[{Line[{{1, 0}, {2, 1}, {3, 0}, {4, 1}}], ImageSize -> 230, 
  AspectRatio -> 1/2,
  Text[Style["1+1", Red, Large, Bold], {1/3 4, 7/8 1}, 
   Background -> LightRed]}, Frame -> True]

Mathematica graphics

I have added Frame-> True so I could see the coordinates of where to put the text. Look up Text there is also an option for an offset.

Does that help?

  • $\begingroup$ Thank you @Hugh for the answer. Is it possible to fill the space above the line with a solid color up to the upper frame? $\endgroup$
    – nanjun
    May 13, 2019 at 21:08
  • $\begingroup$ All depends what you are trying to do. If you use Plot and express your line as a function then there are fill commands. However, if you are looking for geometric shapes then Polygon may be what you need.I can't be sure what your general case is until you show more details. This may be suitable for another question. $\endgroup$
    – Hugh
    May 14, 2019 at 11:02

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.