10
$\begingroup$

I find myself extremely confused about how Placed works with plot legends. I want my legend to partially hang over one edge of the plot. But

Plot[Sin[x], {x, -1, 1}, PlotLegends -> Placed[{"Sine"}, {.975, .75}]]

produces

enter image description here

which is no good because the label gets cut off. As soon as I increase the first coordinate up to 1, it works okay, but space is really tight in my desired application and I can't afford to waste all that white space under the curve.

I've tried using the optional second pair of coordinates in Placed, but I can't figure out how they work - When I hold the first pair fixed and change the values of the second pair, they seem to be moving my legend in the opposite direction as I would expect (e.g. changing the coordinates to {{1, .75}, {2, 0}} moves the legend to the left), and I also have no idea what's setting the scaling of the second pair of coordinates.

I found the question Legend placement explanation, but I can't follow the example because it has so much extraneous crap in it and the answers don't go into enough detail. Not much detail in the documentation for Placed either.

Would this maybe be a job for Show, Inset, or Overlay? I'm not really sure about their relative advantages.

Finally, I noticed that using Placed changes the legend's text from left-aligned to centered. How do I change it back?

Edit: Regarding my last question, the easiest way to change the text alignment of the legend is to change {"Sine"} to LineLegend[{"Sine"}, LabelStyle -> {TextAlignment -> Left}].

$\endgroup$
  • $\begingroup$ It's because it is hanging out of the plot area and being clipped. By adding the options PlotRangeClipping -> False, ImagePadding -> {{Automatic, 20}, {Automatic, Automatic}} you can prevent clipping and extend the 'plot' region to encompass the whole label. $\endgroup$ – Quantum_Oli Sep 15 '16 at 7:17
12
$\begingroup$

tl;dr Use Plot[Sin[x], {x, -1, 1}, PlotLegends -> Placed[{"Sine"}, {{1, .75}, {0.7, 0.5}}]], where {1, 0.75} must be a point outside the $((0,1),(0,1))$ box. {0.7, 0.5} is the scaled position of this point within the legend.


The explanation for this behaviour is tricky and non-obvious.

Graphics with legends have the (possibly nested) structure Legended[graphics, legend]. See this answer for more details.

The Legended expression doesn't evaluate to something, it is simply displayed in a certain way.

However, the display/formatting can happen in two very different ways: if the legend is within the boundaries of the graphics, then the object that is produced is just a single graphics in box representation. The legend is contained within. If the legend is outside, then there will be two separate notebook objects joined together by something which behaves like Grid. I am not 100% certain that all this is correct, but I am convinced that something very similar to this is happening.

The main point is that the result behaves in a different way depending on whether the legend is considered to be inside or outside of the graphics area.

You want an overlapping legend. Now is that inside or outside? You are using scaled coordinates, and both the x and y coordinates fall within $0..1$. That counts as inside. So the legend will be inside.

By default, anything beyond the graphics frame is clipped. This is controlled by PlotRangeClipping. You can set it to False and see that less of the legend is clipped. To allow all of it to show, you would need to manually set ImagePadding to leave enough space.

But this is a lot of trouble and not the easiest way to achieve your goal, so I won't bother fussing with the padding values and posting the full code ...

Let's do something else instead. Look up Placed, expand the Details section and check the Placed[expr, {pos,epos}] syntax. pos is the (scaled) coordinates of a point in the main graphics and epos is the (scaled) coordinates of the same point in the legend. We can achieve an overlapping look by putting epos in the middle of the legend.

Plot[Sin[x], {x, -1, 1}, PlotLegends -> Placed[{"Sine"}, {{1, .75}, {0.5, 0.5}}]]

enter image description here

Now try to move pos just slightly into the $0..1$ interval and see how the clipping happens due to Legended switching to a different rendering mode.

Plot[Sin[x], {x, -1, 1}, PlotLegends -> Placed[{"Sine"}, {{0.999, .75}, {0.7, 0.5}}]]

enter image description here

$\endgroup$
-1
$\begingroup$

I think

 Plot[Sin[x], {x, -1, 1}, PlotLegends -> Placed["sine", {1, .75}]]

will do the trick.

$\endgroup$
  • $\begingroup$ It does not, for the reason already described in my question. Your suggestion also does not show the curve in the legend. $\endgroup$ – tparker Sep 15 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.