1
$\begingroup$

I am creating two plots and using Show to overlay, however the legend has a very large inter-line spacing. This answer suggests using Image Size but my legend is not a separate graphics object. This other solution usings Spacings seems to have the same issue. That or I cannot figure out how to use Spacings properly, I've tried incorporating it into PlotLegends, but no luck.

I would like to have single-line spacing in the legend, without changing the ImageSize or the font size.

MWE

x = Range[0, 10];
y = x^2 * RandomReal[];

plt1 = ListPlot[list, PlotStyle -> Blue, PlotRange -> {0, 100}, 
  Frame -> True, FrameLabel -> {Style["Distance [m]", 8], "Conc"}, 
  PlotLabel -> Style["Title", 10
    ], ImageSize -> Small, 
  PlotLegends -> Placed[{Style["numerical", 8, Black]}, {Left, Top}], 
  LabelStyle -> Black, FrameTicksStyle -> 8]

plt2 = Plot[x^2, {x, 0, 10}, PlotStyle -> {Thick, Red}, 
  PlotRange -> {0, 100}, Frame -> True, ImageSize -> Small, 
  PlotLegends -> Placed[{Style["analytical", 8, Black]}, {Left, Top}],
   LabelStyle -> Black, FrameTicksStyle -> 8]

Show[plt2, plt1]

enter image description here

$\endgroup$
  • $\begingroup$ Use this PlotLegends -> Placed[{Style["numerical", 8, Black]}, Scaled[{0.27, 0.8}] $\endgroup$ – OkkesDulgerci Nov 4 '19 at 3:23
  • $\begingroup$ @OkkesDulgerci that isn't working for me, I either get Scaled[{0.27, 0.8}] in the legend if I put it before of after the {Left, Top} braces, or it has no impact on the spacing if I try put it within the first set of braces $\endgroup$ – Esme_ Nov 4 '19 at 4:12
1
$\begingroup$

Use Scaled and assume you plot's range is $[0,1]\times[0,1]$

SeedRandom@1;
    x = Range[0, 10];
    list = x^2*RandomReal[];

    plt1 = ListPlot[list, PlotStyle -> Blue, PlotRange -> {0, 100}, 
       Frame -> True, FrameLabel -> {Style["Distance [m]", 8], "Conc"}, 
       PlotLabel -> Style["Title", 10], ImageSize -> Small, 
       PlotLegends -> 
        Placed[{Style["numerical", 8, Black]}, Scaled[{0.27, 0.8}]], 
       LabelStyle -> Black, FrameTicksStyle -> 8];

    plt2 = Plot[x^2, {x, 0, 10}, PlotStyle -> {Thick, Red}, 
       PlotRange -> {0, 100}, Frame -> True, ImageSize -> Small, 
       PlotLegends -> 
        Placed[{Style["analytical", 8, Black]}, Scaled[{0.25, 0.9}]], 
       LabelStyle -> Black, FrameTicksStyle -> 8];

    Show[plt2, plt1]

enter image description here

| improve this answer | |
$\endgroup$
1
$\begingroup$

One way is to accomplish what you're after is through Epilog.

Modifying your original plots

plt1 = ListPlot[y, PlotStyle -> Blue, PlotRange -> {0, 100}, 
  Frame -> True, FrameLabel -> {Style["Distance [m]", 8], "Conc"}, 
  PlotLabel -> Style["Title", 10], ImageSize -> Small(*,
  PlotLegends\[Rule]Placed[{Style["numerical",8,Black]},{Left,Top}]*),
   LabelStyle -> Black, FrameTicksStyle -> 8]

plt2 = Plot[x^2, {x, 0, 10}, PlotStyle -> {Thick, Red}, 
  PlotRange -> {0, 100}, Frame -> True, 
  ImageSize -> Small,(*PlotLegends\[Rule]Placed[{Style["analytical",8,
  Black]},{Left,Top}],*)LabelStyle -> Black, FrameTicksStyle -> 8]

We can define a legend

lgnd = {
   Red, Line[{{.5, 85}, {1, 85}}],
   Text[Style["analytical", Black, 8], {1.2, 85}, {-1, 0}],
   Blue, PointSize[.01], Point[{1., 70}],
   Text[Style["numerical", Black, 8], {1.2, 70}, {-1, 0}],
   };

Which we use

Show[{plt1, plt2}, 
 Epilog -> lgnd
 ]

enter image description here

Playe with point size, line thickness etc to refine.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.