2
$\begingroup$

I have some plots which are obtained from a long numerical integration. I have named each plot and then have tried to show them in a single plot using Show command. Here, for the sake of simplicity I have used some simple functions as my plots as follows

p1 = Plot[x^2, {x, 0, 10}, PlotStyle -> {Black, Dashed}];
p2 = Plot[x^3, {x, 0, 10}, PlotStyle -> {Black, Dotted}];
p3 = Plot[x^4, {x, 0, 10}, PlotStyle -> {Black}];

when I use show

Show[Legended[p1, 
  LineLegend[{Directive[Thickness[0.006], Dashed, Black]}, {"p1"}]], 
 Legended[p2, 
  LineLegend[{Directive[Thickness[0.006], Dotted, Black]}, {"p2"}]], 
 Legended[p3, 
  LineLegend[{Directive[Thickness[0.006], Black]}, {"p3"}]]]

the following image is produced enter image description here

which has ugly legends.

1: Why the length of legend line is different for solid one? and how can I equalize their lengths?

2: How can I change the vertical space of the legends? in this case I want to reduce them.

3: How can I change the position of all legends (as a whole) with respect to the plot? for example top or bottom of the plot?

$\endgroup$
6
  • $\begingroup$ I don't know why, but I know the workaround: use a single LineLegend: Legended[Show[{p1,p2,p3}], LineLegend[{...}, {...}]]. If you can, use a single Plot to plot all three things. This is slightly more convenient and allows you not to have to write the styles twice (one for Plot, one for the legend). $\endgroup$
    – Szabolcs
    Oct 13, 2021 at 7:15
  • $\begingroup$ For 2, see LegendMarkerSize in LineLegend docs; for 3 see Legended / Placed in Legended docs. These work if you have a single legend, not three, as I suggested above. I'll leave a full answer with examples to others. $\endgroup$
    – Szabolcs
    Oct 13, 2021 at 7:16
  • $\begingroup$ Changing the order of the legends was recently answered $\endgroup$
    – Syed
    Oct 13, 2021 at 7:26
  • $\begingroup$ @SzabolcsThanks by the way as I said I can't use a single plot. $\endgroup$
    – Wisdom
    Oct 13, 2021 at 7:26
  • $\begingroup$ @Syed Thanks but this is not my question! $\endgroup$
    – Wisdom
    Oct 13, 2021 at 7:28

1 Answer 1

2
$\begingroup$
show = Show[
    Legended[p1, LineLegend[{Directive[Thickness[0.006], Dashed, Black]}, {"p1"}]], 
    Legended[p2, LineLegend[{Directive[Thickness[0.006], Dotted, Black]}, {"p2"}]], 
    Legended[p3, LineLegend[{Directive[Thickness[0.006], Black]}, {"p3"}]]]; 

Process show to get the main plot and legends and construct a new legend with the option "Spacings" using the extracted information:

combinedplot = show[[1, 1, 1]];

combinedlegend = LineLegend[## & @@ 
    Transpose[Cases[show, LineLegend[a_, b_] :> Flatten@{a, b}, All]], 
   "Spacings" -> {1, 1}];

Legended[combinedplot, combinedlegend] 

enter image description here

Legended[combinedplot,
    Placed[Append[LegendLayout -> {"Row",1}] @ combinedlegend, Top]] 

enter image description here

Replace "Spacings" -> {1, 1} with "Spacings" -> {1, 0} to get:

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks a lot, they are what I wanted $\endgroup$
    – Wisdom
    Oct 13, 2021 at 7:32
  • $\begingroup$ one more question: if I want to hold the legends in the right of plot, but raise a little, how do that? $\endgroup$
    – Wisdom
    Oct 13, 2021 at 7:39
  • $\begingroup$ @Wisdom, try Legended[combinedplot,Placed[ combinedlegend, {After, .6}]]. Play with different values in place of .6 to move the legend up/down. $\endgroup$
    – kglr
    Oct 13, 2021 at 7:47
  • $\begingroup$ You're right, Thanks $\endgroup$
    – Wisdom
    Oct 13, 2021 at 7:50

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.