2
$\begingroup$

I tried the following line in order to write a text in my image.

p1bis = Labeled[p1, Text[Style["(8)", FontSize -> 52]], {{Top, Left}}]

I get the following result:

results

I want to move the text position, and get this :

Desire

How can I change the text position ? I tried the following line but doesn't work... p1bis = Labeled[p1, Text[Style["(8)", FontSize -> 52]], {{0.75, 1}}]

$\endgroup$

3 Answers 3

3
$\begingroup$

You can use Legended (instead of Labeled) which allows finer placement:

p1 = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}];
Legended[p1, Placed[Style["(8)", FontSize -> 52], Scaled[{.12, .9}]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Legended works with jpeg ? $\endgroup$ Commented Feb 11, 2019 at 9:21
  • $\begingroup$ @Bigprophete, it seems to work: try, for example, p1 = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}, FrameLabel -> {{"label1", "label2"}, {"label3", "label4"}}]; leg = Legended[p1, Placed[Style["(8)", FontSize -> 36], Scaled[{.12, .9}]]]; Import[Export["leg.jpg", leg]] $\endgroup$
    – kglr
    Commented Feb 11, 2019 at 18:20
3
$\begingroup$

You are using the wrong command, Labeled is for putting a label outside of a plot.

Instead, use Show, which takes plot options, and add the text with Epilog.

e.g.

p1 = DensityPlot[
   PDF[MultinormalDistribution[{0, 0}, {{2, 0}, {0, 1}}], {x, y}],
   {x, -1, 1}, {y, -1, 1}, ImageSize -> 500, 
   FrameLabel -> 
        {Style["horizontal", FontSize -> 26],Style["vertical", FontSize -> 26]}
   ];
p1bis = Show[p1, 
           Epilog -> Text[Style["(8)", FontSize -> 52], Scaled[{0.2, 0.8}]]]

enter image description here

Instead of using Scaled[{0.2,0.8}], which puts the position as a percentage of the plot area from the top left corner, you can also remove Scaled[] and just put the absolute coordinates in the curly brackets.

$\endgroup$
1
  • $\begingroup$ thanks it works ;) $\endgroup$ Commented Feb 11, 2019 at 8:17
2
$\begingroup$

Does this work for you?

ImageCompose[p1, Graphics[Text[Style["(8)", FontSize -> 52]]], 
 Scaled[{0.32, 0.82}]]

Labeled density plot.

Since your own image won't have the "(8)" in the top left, your coordinates will be slightly different than my own so you will have to play around with those to get it to look right.

The original label is still on there because I just copied your first image above, but it should just have the one label inside the graphic when you run it. If you want a background for your text, you can add that either in Style or just add it as an extra argument to Graphics like

Graphics[{White, Rectangle[Scaled[{0.43, 0.43}], Scaled[{.57, .56}]], 
  Black, Text[Style["(8)", FontSize -> 52]]}]

If you have the original code for the plot, I would recommend adding labels using Epilog but I assume that you only have access to the image itself.

$\endgroup$
1
  • $\begingroup$ thanks, it works too $\endgroup$ Commented Feb 11, 2019 at 8:17

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.