TransWikia.com

How to display label text outside plot range and enable PlotRangeClipping?

Mathematica Asked by Ehab Shoubaki on July 22, 2021

I have been trying to modify a clipped graph by adding a text a label outside the frame, with no success so far.

Code for contour per request to get the rest of the code to run:

    ClearAll[Sbase, Vbase, Zbase, countour];
    Sbase = 50 10^3;
    Vbase = 240;
    Zbase = Vbase^2/Sbase;
    countour[M_, Zpu_, XR_] := 
        Circle[{M/Z Cos[[Phi]], -M/Z Sin[[Phi]]}, 1/Z] /. {Z -> Zpu Zbase, [Phi] -> ArcTan[XR]}

This is my graph:



g1 = Graphics[
   Flatten[
    {Red,
     Table[countour[M, 0.02, 3], {M, 0.98, 1.02, 0.001}],
     Blue,
     Table[countour[M, 0.04, 1], {M, 0.98, 1.02, 0.001}]
     }
    ],
   Axes -> True, PlotRangeClipping -> True, AxesStyle -> Thick, 
   AxesLabel -> {x, y}, ImagePadding -> {{All, 50}, {All, All}}, 
   Frame -> True, PlotRange -> {{-0.3, 0.3}, {-0.3, 0.3}}];

The contour function just produces circles with differing centers and radii. I am zooming in close to the origin and clipping everything else to get this:

Frame with clipping

All is fine up to now, but I also want to add text labels outside the clipped frame. My successful trials, along with what I see suggested online(including StackExchange), hinge on setting PlotRangeClipping to False, for example as suggested here: "insert a text outside the frame".

That does not work for me, because when I try it I get:

ClearAll[g1, addLabel];
addLabel = Show[#,
    Epilog -> Text[#2, Scaled[{1.02, 0.7}], {-1, -1}],
    PlotRangeClipping -> False, 
    ImagePadding -> {{All, 50}, {All, All}}] &;
g1 = Graphics[
   Flatten[
    {Red,
     Table[countour[M, 0.02, 3], {M, 0.98, 1.02, 0.001}],
     Blue,
     Table[countour[M, 0.04, 1], {M, 0.98, 1.02, 0.001}]
     }
    ],
   Axes -> True, AxesStyle -> Thick, AxesLabel -> {x, y}, 
   Frame -> True, PlotRange -> {{-0.3, 0.3}, {-0.3, 0.3}}];
addLabel[g1, Style["M[Equal]1", 16, Red]]

enter image description here

Which is not what I want at all. If I try to keep PlotRangeClipping as ‘True` I lose the text label.

Any recommended workarounds ?

2 Answers

First, create a graphics object with your primitives, using the desired plot range, with clipping and no image padding:

g1 = Graphics[
    Flatten[{
        Red,
        Table[countour[M,0.02,3],{M,0.98,1.02,0.001}],
        Blue,
        Table[countour[M,0.04,1],{M,0.98,1.02,0.001}]
    }],
    PlotRangeClipping->True,
    ImagePadding->0,
    PlotRange->{{-0.3,0.3},{-0.3,0.3}}
];

Then, inset this graphics object into your graphic, along with the text labels and your overall axes/frame specifications:

Graphics[
    {
    Inset[g1, {0, 0}, {0, 0}, {.6, .6}],
    Text[Style["M[Equal]1",16,Red],Scaled[{1.02,0.7}],{-1,-1}]
    },
    PlotRange -> {{-.3, .3}, {-.3, .3}},
    ImagePadding -> {{Automatic, 50}, {Automatic, Automatic}},
    Frame -> True,
    Axes -> True,
    AxesLabel -> {x,y},
    AxesStyle -> Thick
]

enter image description here

Correct answer by Carl Woll on July 22, 2021

You can also clip the graphics primitives:

ClearAll[clip]
clip[prange_] := RegionIntersection[Rectangle @@ Transpose[prange], #] /. _EmptyRegion -> {} &;

Wrap your countour[...]s with clip[plotrange]

plotrange = {{-0.3, 0.3}, {-0.3, 0.3}};

g2 = Graphics[Flatten[{Red, 
     Table[clip[plotrange] @ countour[M, 0.02, 3], {M, 0.98, 1.02, 0.001}], 
     Blue, Table[clip[plotrange] @ countour[M, 0.04, 1], {M, 0.98, 1.02, 0.001}]}], 
   Axes -> True, AxesStyle -> Thick, 
   AxesLabel -> {x, y}, Frame -> True, PlotRange -> plotrange];

addLabel[g2, Style["M[Equal]1", 16, Red]]

enter image description here

Caveat: This approach does not work with primitives like FilledCurve, BezierCurve, BSPlineCurve, and transformed primitives (e.g. primitives wrapped inside GeometricTransformation, Translate, Rotate, Scale etc.) which do not work with RegionIntersection.

Answered by kglr on July 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP