6

I'm trying to include the legends inside the frame of the plot like this

hehe

Here is my Attempt:

ListPlot[{{2, 5, 2, 8, 6, 8, 3}, {1, 2, 5, 2, 3, 4, 3}}, 
 PlotMarkers -> {"\[SixPointedStar]", 15}, Joined -> True, 
 PlotStyle -> {Orange, Green}, 
 PlotLegends -> 
  Placed["line1", "line2", 
   LegendFunction -> (Framed[#, FrameMargins -> 0] &)], Frame -> True]

My references:

  1. specify-legend-position-in-a-plot
  2. plotting-legends-matching-with-plots-inside-the-show-graph
  3. placing-plot-legends-inside-a-plot
  4. place-a-legend-inside-a-plot.
user516076
  • 373
  • 1
  • 8

3 Answers3

10

You did not put any location for Placed

ListPlot[{{2, 5, 2, 8, 6, 8, 3}, {1, 2, 5, 2, 3, 4, 3}}, 
 PlotMarkers -> {"\[SixPointedStar]", 15}, Joined -> True, 
 PlotStyle -> {Orange, Green}, 
 PlotLegends -> Placed[LineLegend[{"line1", "line2"}, 
 LegendFunction -> (Framed[#, FrameMargins -> 0] &)], {0.1, 0.5}], 
Frame -> True]

enter image description here

Sumit
  • 15,912
  • 2
  • 31
  • 73
9

Space for the legend is available at the upper left

ListPlot[{
  {2, 5, 2, 8, 6, 8, 3},
  {1, 2, 5, 2, 3, 4, 3}},
 PlotMarkers -> {"✶", 15},
 Joined -> True,
 PlotStyle -> {Orange, Green},
 PlotLegends ->
  Placed[
   LineLegend[{"line1", "line2"},
    LegendFunction -> "Frame"],
   {.15, .8}],
 Frame -> True]

enter image description here

To locate legend at upper right you must change the PlotRange

ListPlot[{
  {2, 5, 2, 8, 6, 8, 3},
  {1, 2, 5, 2, 3, 4, 3}},
 PlotRange -> {{0, 9}, {0, 9}},
 PlotMarkers -> {"✶", 15},
 Joined -> True,
 PlotStyle -> {Orange, Green},
 PlotLegends ->
  Placed[
   LineLegend[{"line1", "line2"},
    LegendFunction -> "Frame"],
   {.85, .8}],
 Frame -> True]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
6

Often these graphics commands are a bit obscure and one has to try. Is the following approx. what you are looking for?:

ListPlot[{{2, 5, 2, 8, 6, 8, 3}, {1, 2, 5, 2, 3, 4, 3}}, 
 PlotMarkers -> {"\[SixPointedStar]", 15}, Joined -> True, 
 PlotStyle -> {Orange, Green}, 
 PlotLegends -> 
  Placed[LineLegend[{"line1", "line2"}, 
    LegendFunction -> Framed], {0.85, 0.8}], Frame -> True, 
 PlotRange -> {{0, 10}, All}]

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57