How to organize these plot legends?

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

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?

• 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). Oct 13, 2021 at 7:15
• 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. Oct 13, 2021 at 7:16
• Changing the order of the legends was recently answered
– Syed
Oct 13, 2021 at 7:26
• @SzabolcsThanks by the way as I said I can't use a single plot. Oct 13, 2021 at 7:26
• @Syed Thanks but this is not my question! Oct 13, 2021 at 7:28

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]


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


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

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