TransWikia.com

Include the legend inside the plot but was failed

Mathematica Asked on March 8, 2021

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.

3 Answers

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

Correct answer by Bob Hanlon on March 8, 2021

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

Answered by Sumit on March 8, 2021

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

Answered by Daniel Huber on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP