3
$\begingroup$

I want to place a Text (or some other Graphics object) outside the PlotRange using the Epilog option. I know that I can do this by adding some ImagePadding and using PlotRangeClipping -> False (see eg. this post).

However, PlotRangeClipping->False is not an option for me, because in the final Graphics object I need to combine several plots using Show with a new PlotRange. The individual plots extend this range, and therefore the plots will leak out of the desired plot region.

Is there a way to place the Text outside the PlotRange without using PlotRangeClipping -> False?

As a minimal example to see the unwanted behavior I create two plots:

p1 = ListLinePlot[{{-1, 1}, {10, 3}}, PlotRange -> All];
p2 = ListLinePlot[{{1, 1}, {10, 10}}, PlotRange -> All];

and use Show to display both with the additional Text:

Show[{p1, p2}, 
 PlotRange -> {{0, 5}, {0, 8}}, 
 Frame -> True, 
 PlotRangeClipping -> False, 
 ImagePadding -> {{All, 50}, {All, All}}, 
 Epilog -> Style[Text["A", Scaled[{1.1, 0.5}]], Red, Large]
]

unwanted behavior due to PlotRangeClipping->False

$\endgroup$

1 Answer 1

3
$\begingroup$

I ended up with creating a Rectangle on which I placed the required elements. For the placement of the elements, I found it most convenient to specify absolute values for the width and height of the rectangle as well as the ImageSize of the plots.

First create the two plots:

p1 = ListLinePlot[{{-1, 1}, {10, 3}}, PlotRange -> All];
p2 = ListLinePlot[{{1, 1}, {10, 10}}, PlotRange -> All];

Then create a Graphics object that serves as a canvas:

widthCanvas = 300;
heightCanvas = 170;
canvas = Graphics[{
        White, 
        Rectangle[{0, 0}, {widthCanvas, heightCanvas}]
    },
    PlotRangePadding -> 0
];

Combine the two plots:

widthPlots = 250;
plotsCombined = Show[
    {p1, p2}, 
    PlotRange -> {{0, 5}, {0, 8}}, 
    Frame -> True, 
    ImageSize -> widthPlots
]

Finally Show the canvas, add the Text and place the combined plots as an Inset in the Epilog option

Show[canvas, Epilog -> {
    Inset[plotsCombined, Offset[{15, 15}], Scaled[{0, 0}]],
    Style[Text["A", Scaled[{0.95, 0.5}]], Red, Large]
}]

canvas

PS: in this image, I used the background color LightOrange instead of White to visualize the canvas boundaries.

$\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.