6
$\begingroup$

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.
$\endgroup$

3 Answers 3

9
$\begingroup$

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

$\endgroup$
10
$\begingroup$

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

$\endgroup$
6
$\begingroup$

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}]

$\endgroup$

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.