6
$\begingroup$

is it possible to insert a text outside the frame box?

I mean on the top left.

as shown in the following picture.

enter image description here

$\endgroup$
6
  • 1
    $\begingroup$ Plot[x, {x, 0, 1}, Frame -> True, Epilog -> Text[StringForm["x ``", 10^"-3"], {0.05, 1.05}], PlotRange -> {0, 1}, ImagePadding -> 20, PlotRangeClipping -> False] $\endgroup$
    – Bob Hanlon
    Commented Oct 30, 2020 at 17:26
  • $\begingroup$ @Bob Hanlon Thanks, but it this solution is not general, and it should be altered depending on the type of the plot $\endgroup$
    – user68857
    Commented Oct 30, 2020 at 17:46
  • $\begingroup$ For a more general solution used Scaled coordinates, e.g., Text[StringForm["x ``", 10^"-3"], Scaled[{0.07, 1.05}]] You should clarify what different types of plots to which you refer. $\endgroup$
    – Bob Hanlon
    Commented Oct 30, 2020 at 17:53
  • $\begingroup$ @Bob Hanlon Thank you very much $\endgroup$
    – user68857
    Commented Oct 30, 2020 at 17:55
  • 1
    $\begingroup$ You can also combine with graphics primitives using Show[{ your plot ,Graphics[{graphics primitives}]}]. For example Show[{Plot[{Log[x], 2 Log[x]}, {x, .1, 1.5}, AxesOrigin -> {0, 1}],Graphics[{White, Point[{-.2, 1.4}], Black, Text[StringForm["x``", 10^"-3"], {-.1, 1.3}]}]}, PlotRange -> All] $\endgroup$
    – Nicholas G
    Commented Oct 30, 2020 at 18:09

2 Answers 2

8
$\begingroup$
ClearAll[addLabel]
addLabel = Show[#, 
    Epilog -> Text[#2, Scaled[{0, 1}], {-1, -1}], 
    PlotRangeClipping -> False, ImagePadding -> 30] &;

Examples:

plt1 = Plot[x, {x, -1, 1}, Frame -> True, ImageSize -> Medium];
plt2 = Plot[ 3 Sin @ x, {x, -3 Pi, Pi}, Frame -> True, 
   AspectRatio -> 1, ImageSize -> Medium];
plt3 = ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, ImageSize -> Medium];
plt4 = Histogram[RandomVariate[NormalDistribution[0, 1], 200], 
   Frame -> True, ImageSize -> Medium];
plt5 = RegionPlot[1/4 <= x^2 + y^2 <= 3, {x, -2, 2}, {y, -2, 2}, ImageSize -> Medium];
plt6 = BarChart[{1, 2, 3}, Frame -> True, ImageSize -> Medium];

plots = {plt1, plt2, plt3, plt4, plt5, plt6};

Multicolumn[addLabel[#, Style[Row[{"x", 10^"-3"}], 16]] & /@ plots, 3]

enter image description here

Use Text[#2, Scaled[{1, 1}], {1, -1}] to get:

enter image description here

Use Text[#2, Scaled[{1, 1}], {1, 1.5}, {0, 1}] to get:

enter image description here

Use Text[#2, Scaled[{1, 0}], {-1, 1.5}, {0, 1}] to get:

enter image description here

$\endgroup$
1
$\begingroup$

I'm a little late to the party, but I find any time I'm forced to deal with PlotRangeClipping things start to get janky. My preferred way of doing this via the Overlay[] function.

It's lightweight, flexible, intuitive, and you can leave whatever plots you have as-is. If you need to add a little bit of ImagePadding, you can do so explicitly when you add the Overlay.

(*Plot Code*)
plt3 = ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, ImageSize -> Medium];

(*Overlay Code*)
Overlay[{
  Show[plt3, ImagePadding -> {{Automatic, Automatic}, {Automatic, 15}}],
  Style[Row[{"x", 10^"-3"}], Black, FontSize -> 12]},
 Alignment -> {-0.91, 1}]

Which produces the following.

enter image description here

Alignment isn't explained super well in the Overlay documentation, so for those that need it:

Alignment->{-1,-1} is bottom left,

Alignment->{1,1} is top right,

Alignment->{0,0} is in the middle (not of the graph, but the entire graphic/plot).

$\endgroup$

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.