1
$\begingroup$

I am trying to place my plot legend inside the graph. Currently it sits outside the plot and gets covered up when I place another plot next to it.

enter image description here

This is the code I am using.

plot1 = Show[{dateListPlot, fitPlot, fitPlot4, fitPlot3, fitPlot2}, ImageSize -> 900];
Legended[plot1, LineLegend[{ Cyan, Purple, Green}, {"21", "14", "7"}, LegendLayout -> {"Column", 1}]]
histogram = Histogram[values, {2}];
GraphicsRow[{plot1, histogram}]
$\endgroup$
0

2 Answers 2

3
$\begingroup$

You can also put your LineLegend in an Inset on Prolog (or Epilog) with correct placement (Scaled parts for Inset):

Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi},
 Prolog ->
  Inset[
   LineLegend[
    (* Default colors acquired awkwardly... *)
    Take[ColorData[97, "ColorList"], 2], {Sin[x], Cos[x]}],
   Scaled[{3/4, 1}], Scaled[{1, 1}]]]

enter image description here

$\endgroup$
2
$\begingroup$

I will try to create a minimal example with some randomly created data:

SeedRandom[3]; data = RandomReal[1, {10, 2}];
plot1 = Legended[
  ListLinePlot[Sort@data, ImageSize -> Medium]
  , Placed[
   LineLegend[{Cyan, Purple, Green}, {"21", "14", "7"}, 
    LegendLayout -> {"Column", 1}], {0.8, 0.8}]
  ]

histogram = Histogram[data];
GraphicsGrid[{{plot1, histogram}}, ImageSize -> 600]

enter image description here

$\endgroup$
1
  • $\begingroup$ More straightforward, SeedRandom[3]; data = Sort@RandomReal[1, {3, 10}]; plot1 = ListLinePlot[data, ImageSize -> Medium, PlotStyle -> {Cyan, Purple, Green}, PlotLegends -> Placed[LineLegend[{"21", "14", "7"}], {0.65, 0.8}]] $\endgroup$
    – Bob Hanlon
    Apr 6, 2022 at 16:44

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.