8
$\begingroup$

I would like to place the legend inside the plot frame. I have been doing a search in the documentation but only found option

ChartLegends -> Placed[{"label", {Center}]

Other options, such as Up, Bottom, Right, place the label outside.

How can I put the legend inside the frame of the plot?

$\endgroup$
1
  • $\begingroup$ You refer to "plot" but the only code provided applies to a "chart". You might want to clarify what you are actually trying to accomplish. $\endgroup$
    – Bob Hanlon
    Apr 3, 2017 at 19:07

1 Answer 1

14
$\begingroup$
Legended[
 BarChart[{{4, 5, 6}, {1, 2, 3}},
  ChartStyle -> {"Pastel", None}],
 Placed[
  SwatchLegend[
   ColorData["Pastel", #] & /@ {0, 1},
   {"Group A", "Group B"}],
  {0.8, 0.8}]]

enter image description here

EDIT: More simply,

BarChart[{{4, 5, 6}, {1, 2, 3}},
 ChartStyle -> {"Pastel", None},
 ChartLegends -> Placed[
   {{"Group A", "Group B"}, None},
   {0.8, 0.8}]]

enter image description here

EDIT 2: Using SmoothHistogram

SmoothHistogram[
 Table[
  RandomVariate[NormalDistribution[c, 1], 500],
  {c, 0, 5}],
 PlotLegends -> Placed[Range[0, 5], {0.9, 0.7}]]

enter image description here

$\endgroup$
7
  • $\begingroup$ Thank you very much for this solution. I also wanted to ask the same question but using PlotLegends option of SmoothHistogram. I have tried this solution but does not work with PlotLegends. Regards. $\endgroup$ Apr 3, 2017 at 19:26
  • $\begingroup$ See EDIT 2. When asking a question, you need to post code for minimal working example that demonstrates issue that you are having. $\endgroup$
    – Bob Hanlon
    Apr 3, 2017 at 19:52
  • 2
    $\begingroup$ Thank you, the solutions have solved this problem. I use PlotTheme -> "Scientific", ChartStyle -> 96, ChartBaseStyle -> Directive[EdgeForm[None], Opacity[0.25]]in the Histogram then I have put Placed[ SwatchLegend[96, {"No stigmergy", "FPS"}], {0.9, 0.8}]in my code but the color of the legend looks darker (intense) than the plot lines. Do you know how I could match both, plot lines and legend colors?. Thank you for your help. $\endgroup$ Apr 3, 2017 at 22:24
  • $\begingroup$ @user1993416 - Post a new question that includes the actual data and code that you are using and the issue that you want resolved. $\endgroup$
    – Bob Hanlon
    Apr 3, 2017 at 22:49
  • $\begingroup$ @BobHanlon, very nice! What if we want a framed legend, e.g. legend = Placed[LineLegend[l, LegendFunction -> "Frame"], {.6, .4}]; with white space, rather than gridlines (needed for plot) within the legend frame? $\endgroup$
    – mjw
    May 3, 2022 at 22:56

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.