8

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user1993416
  • 589
  • 1
  • 5
  • 12
  • 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. – Bob Hanlon Apr 03 '17 at 19:07

1 Answers1

14
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

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • 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. – user1993416 Apr 03 '17 at 19:26
  • See EDIT 2. When asking a question, you need to post code for minimal working example that demonstrates issue that you are having. – Bob Hanlon Apr 03 '17 at 19:52
  • 2
    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. – user1993416 Apr 03 '17 at 22:24
  • @user1993416 - Post a new question that includes the actual data and code that you are using and the issue that you want resolved. – Bob Hanlon Apr 03 '17 at 22:49
  • @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? – mjw May 03 '22 at 22:56
  • @mjw - do not ask questions in comments. Post a question with your code for a complete minimal working example that shows the problem you are trying to solve. – Bob Hanlon May 03 '22 at 23:00
  • @BobHanlon, okay, will do ... – mjw May 03 '22 at 23:01