Group Abstract Group Abstract

Message Boards Message Boards

0
|
50 Views
|
1 Reply
|
1 Total Like
View groups...
Share
Share this post:

Could PlotLegends->Placed[] inconsistency be fixed?

Posted 1 day ago

The documentation for PlotLegends specifies that the position of the legend can be manually controlled using Placed:

PlotLegends->Placed[leg,pos] places the legend leg at the position specified by pos. pos in a form of {{ex,ey},{lx,ly}}: scaled position {lx,ly} in the legend at scaled position {ex,ey} in expr.

However, the behaviour seems to have a discrepancy for ex or ey being below 1 or more or equal to 1. Consider the following example:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]},
 Frame -> True,
 FrameLabel -> {{"y", "y"}, {"x", "Trigonometric functions"}},
 ImageSize -> 400,
 PlotLegends -> 
  Placed[LineLegend["Expressions", 
    LegendFunction -> (Framed[#1, FrameMargins -> 0] & )],
   {{1 - 0.0001, 1 - 0.0001}, {1, 1}}]]

This results in legend being inside the Plot's frame, and almost-but-not-quite aligned with it: Plot with legend within the Plot's Frame

However, if one wants to actually align the legend to the corner frame, this happens:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]},
 Frame -> True,
 FrameLabel -> {{"y", "y"}, {"x", "Trigonometric functions"}},
 ImageSize -> 400,
 PlotLegends -> 
  Placed[LineLegend["Expressions", 
    LegendFunction -> (Framed[#1, FrameMargins -> 0] & )],
   {{1, 1}, {1, 1}}]]

The legend ends up aligning to the corner of the expression's frame (while also making the plot itself smaller)!

enter image description here

Is it possible to make this behaviour consistent? It would be amazing to be able to align to the Plot's frame rather than to the Expression's frame by default for both cases, even better to have it as an option, but even a simpler fix (everything always aligns to the Expression's frame) would be great

POSTED BY: Anton Lutsenko

i think the problem is that Frame is not a Graphics objects. The coordinate issue (which is a bug I guess) can be solved by doing:

Placed[… , {Scaled[{1, 1}], Scaled[{1, 1}]}]

And not-so-nice solution would be to add the 'box' manually:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, Frame -> True, 
 FrameLabel -> {{"y", "y"}, {"x", "Trigonometric functions"}}, 
 ImageSize -> 400, PlotRange -> {{0, 2 Pi}, {-1.1, 1.2}}, 
 PlotRangePadding -> None, 
 PlotLegends -> 
  Placed[LineLegend["Expressions"], {Scaled[{1, 1}], Scaled[{1, 1}]}],
  Epilog -> {EdgeForm[Black], FaceForm[], 
   Line[{{5, 1.2}, {5, 0.65}, {2 Pi, 0.65}}]}, FrameStyle -> Black]
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard