5
$\begingroup$

I am trying to plot some datasets ({L1, L2, L3, L4, L5, L6, L7, L8, L9, atmve, atmvebar, atmvmu, atmvmubar, DSNve, DSNvebar, DSNvx}) using ListLogLogPlot. For that I have written this following code-:

ListLogLogPlot[{L1, L2, L3, L4, L5, L6, L7, 
  L8, L9, atmve, atmvebar, 
  atmv\[Mu], atmv\[Mu]bar, DSNve, DSNvebar, DSNvx}, 
 PlotRange -> {{10^-1, 10^3}, {10^-4, 10^12}}, 
 PlotRangeClipping -> True, AspectRatio -> 0.8, Frame -> True, 
 Joined -> True, 
 BaseStyle -> Directive[Bold, FontFamily -> "Times", 16], 
 FrameStyle -> Directive[Black, Thick], ImageSize -> 400, 
 FrameLabel -> {"\!\(\*SubscriptBox[\(E\), \(\[Nu]\)]\)(MeV)", 
   "Differential flux (\!\(\*SuperscriptBox[\(MeV\), \
\(-1\)]\)\!\(\*SuperscriptBox[\(s\), \
\(-1\)]\)\!\(\*SuperscriptBox[\(cm\), \(-2\)]\))"}, 
 PlotStyle -> {Directive[Darker[Black], Thick], Directive[Red, Thick],
    Directive[Lighter[Blue], Thick], Directive[Darker[Green], Thick], 
   Directive[Darker[Magenta], Thick], 
   Directive[Darker[Orange], Thick], 
   Directive[Darker[Black], Thick, Dashed], 
   Directive[Red, Thick, Dashed], 
   Directive[Lighter[Blue], Thick, Dashed], 
   Directive[Darker[Green], Thick, Dashed], 
   Directive[Darker[Magenta], Thick, Dashed], 
   Directive[Darker[Orange], Thick, Dashed], 
   Directive[Darker[Black], Thick, DotDashed], 
   Directive[Red, Thick, DotDashed], 
   Directive[Lighter[Blue], Thick, DotDashed], 
   Directive[Darker[Green], Thick, DotDashed]}, FrameTicks -> ticks1, 
 PlotLegends -> 
  Placed[LineLegend[{Directive[Darker[Black], Thick], 
     Directive[Red, Thick], Directive[Lighter[Blue], Thick], 
     Directive[Darker[Green], Thick], 
     Directive[Darker[Magenta], Thick], 
     Directive[Darker[Orange], Thick], 
     Directive[Darker[Black], Thick, Dashed], 
     Directive[Red, Thick, Dashed], 
     Directive[Lighter[Blue], Thick, Dashed], 
     Directive[Darker[Green], Thick, Dashed], 
     Directive[Darker[Magenta], Thick, Dashed], 
     Directive[Darker[Orange], Thick, Dashed], 
     Directive[Darker[Black], Thick, DotDashed], 
     Directive[Red, Thick, DotDashed], 
     Directive[Lighter[Blue], Thick, DotDashed], 
     Directive[Darker[Green], Thick, DotDashed]}, {"pp", "hep", 
     "\!\(\*SuperscriptBox[\(\\\ \), \(13\)]\)N", 
     "\!\(\*SuperscriptBox[\(\\\ \), \(15\)]\)O", 
     "\!\(\*SuperscriptBox[\(\\\ \), \(17\)]\)F", 
     "\!\(\*SuperscriptBox[\(\\\ \), \(8\)]\)B", 
     "\!\(\*SuperscriptBox[\(\\\ \), \
\(7\)]\)\!\(\*SubscriptBox[\(Be\), \(861.3\)]\)", 
     "\!\(\*SuperscriptBox[\(\\\ \), \
\(7\)]\)\!\(\*SubscriptBox[\(Be\), \(384.3\)]\)", "pep", 
     "atm: \!\(\*SubscriptBox[\(\[Nu]\), \(e\)]\)", 
     "atm:" Subscript[OverBar[\[Nu]], e], 
     "atm: \!\(\*SubscriptBox[\(\[Nu]\), \(\[Mu]\)]\)", 
     "atm:" Subscript[OverBar[\[Nu]], \[Mu]], 
     "DSN: \!\(\*SubscriptBox[\(\[Nu]\), \(e\)]\)", 
     "DSN:" Subscript[OverBar[\[Nu]], e], 
     "DSN: \!\(\*SubscriptBox[\(\[Nu]\), \(x\)]\)"}, 
    LegendMargins -> {{0.001, 0.001}, {0.0000001, 0.0000001}}, 
    LegendMarkerSize -> {25}, LegendLayout -> {"Row", 2}, 
    LabelStyle -> Directive[Bold, FontFamily -> "Times", 11]], Below]]

Now I get this following output, Where the Legend is in left alignment:-enter image description here

But I want the Legend to be in justify (perfect) alignment in the row. So, can anyone help me how I can do that

$\endgroup$

1 Answer 1

5
$\begingroup$
colors = ColorData[97] /@ Range[15];
labels = Array["label" <> ToString @ # &, 15];

legend = LineLegend[colors, labels, LegendLayout -> {"Row", 2}]

enter image description here

1. You can wrap each label with Pane and specify the same ImageSize:

is = Map[Max]@Transpose[Rasterize[#, "RasterSize"] & /@ labels]
{49, 15}
pane = Pane[#, ImageSize -> is] & ;

LineLegend[colors, pane  /@ labels, LegendLayout -> {"Row", 2}]

enter image description here

Alternatively, post-process legend to wrap each label with pane:

MapAt[Map[pane], legend, {2}]

enter image description here

2. You can use a custom function as the setting for LegendLayout:

legendlayout = Grid[Row[#, Spacer[2]] & /@ # & /@ Partition[#, 8, 8, 1, {}], 
    Alignment -> Left, Spacings -> {1, .5}] &;

LineLegend[colors, labels, LegendLayout -> legendlayout]

enter image description here

$\endgroup$

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.