Show and PlotLegend row spacing
Update 2: Post-processing plots
without having to create a new legend:
Legended[Show[plots[[;; , 1]], PlotRange -> All],
Column[Join @@ plots[[;; , 2, All, 1]], Spacings -> -.8]]
Legended[Show[plots[[;; , 1]], PlotRange -> All],
Placed[Column[Join @@ plots[[;; , 2, All, 1]], Spacings -> -.8], {Before, Top}]]
Original post:
Use the (afaik) undocumented option "Spacings"
for LineLegend
:
legend = LineLegend[Hue[#/11] & /@ Range[10], ("Serial " <> ToString[#]) & /@ Range[10],
LabelStyle -> {FontFamily -> "Times", 10},
"Spacings" -> {1, .2}]
or use LegendLayout
with a custom layout function:
legend = LineLegend[Hue[#/11] & /@ Range[10], ("Serial " <> ToString[#]) & /@ Range[10],
LabelStyle -> {FontFamily -> "Times", 10},
LegendLayout -> (Grid[#, Spacings -> {1, .2}] &)]
to get
You can then use legend
with Legended
Legended[Plot[(# x) & /@ Range[10], {x, 0, 1}, Evaluated -> True,
PlotStyle -> (Hue[#/11] & /@ Range[10]), Frame -> True,
FrameLabel -> {"x", "y"}, ImageSize -> 4 72,
BaseStyle -> {FontFamily -> "Times", FontSize -> 10}], legend]
or with PlotLegends
Plot[(# x) & /@ Range[10], {x, 0, 1}, Evaluated -> True,
PlotStyle -> (Hue[#/11] & /@ Range[10]), Frame -> True,
FrameLabel -> {"x", "y"}, ImageSize -> 4 72,
BaseStyle -> {FontFamily -> "Times", FontSize -> 10},
PlotLegends -> legend]
we get
Update: If you do not want to redo the Plot
s you can use your plots
with legend
as follows:
Legended[Show[plots[[;;, 1]], PlotRange -> All], legend]
Sometimes, you may not want to reconstruct the legend from scratch then you can extract the legends from the plots, and make adjustments on them, for example
p=Show[plots, PlotRange -> All];
opt = First@Cases[plots[[1]], LineLegend[x_List, y_List, opt__] :> {opt}, ∞];
lgd = LineLegend[
Sequence @@
Transpose[
First@Cases[#,
LineLegend[x_List, y_List, opt__] :> {x, y}, ∞] & /@
plots], Sequence @@ FilterRules[opt, Except[LegendLayout]],
LegendLayout -> {"Column", 2}]
Legended[First@Cases[p, Graphics[___], ∞], lgd]
Or
lgd = LineLegend[
Sequence @@
Transpose[
First@Cases[#,
LineLegend[x_List, y_List, opt__] :> {x, y}, ∞] & /@
plots], Sequence @@ FilterRules[opt, Except[LegendLayout]],
LegendLayout -> {"Row", 2}]
Legended[First@Cases[p, Graphics[___], ∞], Placed[lgd, Bottom]]
The option LegendMargins
can be used to significantly reduce the spacing.
This option can be added directly to LineLegend
plots = Table[
Plot[x i, {x, 0, 1}, PlotStyle -> {Hue[i/11]},
PlotLegends ->
LineLegend[{"Serial " <> ToString[i]}, LabelStyle -> {FontFamily -> "Times", 10},
LegendMargins -> {0, 0}],
Frame -> True, FrameLabel -> {"x", "y"}, ImageSize -> 4 72,
BaseStyle -> {FontFamily -> "Times", FontSize -> 10}], {i, 10}];
Show[plots, PlotRange -> All]
or after the plots
were produced.
plots = Table[
Plot[x i, {x, 0, 1}, PlotStyle -> {Hue[i/11]},
PlotLegends ->
LineLegend[{"Serial " <> ToString[i]}, LabelStyle -> {FontFamily -> "Times", 10}],
Frame -> True, FrameLabel -> {"x", "y"}, ImageSize -> 4 72,
BaseStyle -> {FontFamily -> "Times", FontSize -> 10}], {i, 10}];
Show[plots /. LineLegend[ll__] :> LineLegend[ll, LegendMargins -> {0, 0}], PlotRange -> All]
To have the legends starting at the top, one additional replacement is needed.
Show[plots /.
LineLegend[ll__] :> LineLegend[ll, LegendMargins -> {0, 0}] /. After -> {After, Top},
PlotRange -> All]
Negative margin values reduce the spacing even further.
Show[plots /. LineLegend[ll__] :> LineLegend[ll, LegendMargins -> {{5, 0}, {-5, -5}}] /.
After -> {After, Top}, PlotRange -> All]