TransWikia.com

How do I control the spacing and font size for my plot legend?

Mathematica Asked on July 14, 2021

I have a Manipulate-based figure in which I’d like to place a compact legend in the upper left corner (within the figure’s frame). But by default, the items in the legend are spaced too far apart, and have a font that’s too big (and different from the font used for the axes and other labels):

enter image description here

How do I control the spacing and font size for my plot legend? I’ve tried all the answers I’ve found here that seem relevant, but I fear that even if I hit on the potentially right one, it isn’t clear where to apply the solution in the context of my code.

Ideally, I’d like a solution that simplifies the code as a whole, or at least doesn’t make it more complex. I’ve long passed the point I often reach in Mathematica where I think: “I should have just done this in Python!”


sigmoid[z_]:= E^z/(E^z+1)
relu[z_]:=Max[{0, #}]&/@z
softplus[z_]:=Log[1+E^z]

Manipulate[
total=E^rng/saturation;
Show[{
        Plot[{E^z/total},  {z, -rng, Log[saturation*total](*==rng*)},  PlotRange->{{-rng, rng},  {0, prng}}, 
            PlotLegends->Placed[{StringForm["Softmax (`1`%)", NumberForm[saturation*100, {3, 1}]]}, {Left, Top}], PlotStyle->Blue, 
            Epilog->{Text[Style[N[E^rng/total, 4]], {rng+.3,  E^rng/total}, {-0.5, 0}]}], 
        Plot[{sigmoid@z},  {z, -rng, rng},  PlotRange->{{-rng, rng},  {0, prng}}, 
            PlotLegends->Placed[{"Sigmoid"}, {Left, Top}],  PlotStyle->Orange], 
        Plot[{relu@z}, {z, -rng, rng}, PlotRange->{{-rng, rng},  {0, prng}}, 
            PlotLegends->Placed[{"ReLU"}, {Left, Top}], PlotStyle->Purple], 
        Plot[{softplus@z}, {z, -rng, rng}, PlotRange->{{-rng, rng},  {0, prng}}, 
            PlotLegends->Placed[{"Softplus"}, {Left, Top}], PlotStyle->Green]
    }, FrameLabel->{"z", "a(z)"}, AxesOrigin->{-rng, 0}, Frame->True, 
    Prolog->{GrayLevel[.95], Line[{{0, 0}, {0, prng}}],  Dashed,  Line[{{-rng, 1.0}, {rng, 1.0}}]}, 
    PlotRangeClipping->False, ImagePadding->{{Automatic, 45}, {Automatic, Automatic}}], 
    {{rng, 5,  "Logit (z) range"}, 1, 10}, 
    {{saturation,  1, "Logit concentration"}, 1, 1/n, 0.001}, (* Activation,  a(z),  fraction in highest *)
    Delimiter, 
    {{n, 10, "Classes (has no effect)"}, 1, 50, 1,  Visible->False}, 
    {{prng, 1.1, "Activation (a) scale range"}, 1.0, 2*rng}]

2 Answers

you can specify the position and other options of the elements in PlotLegends. you can read the rules in the legends placement section here http://reference.wolfram.com/language/ref/PlotLegends.html

here is an example of your code with fonts ,color,position and spacing modified. I was not able to avoid the "{ }" around the text though, maybe someone else will tell explain it to us

sigmoid[z_] := E^z/(E^z + 1)
relu[z_] := Max[{0, #}] & /@ z
softplus[z_] := Log[1 + E^z]

Manipulate[total = E^rng/saturation;
 Show[{Plot[{E^z/total}, {z, -rng, 
     Log[saturation*total](*[Equal]rng*)}, 
    PlotRange -> {{-rng, rng}, {0, prng}}, 
    PlotLegends -> 
     Placed[{Text[
        Style[StringForm["Softmax (`1`%)", 
          NumberForm[saturation*100, {3, 1}]], Blue, 7]]}, {{0.11, 
        0}, {0, -5.6}}], PlotStyle -> Blue, 
    Epilog -> {Text[
       Style[N[E^rng/total, 4]], {rng + .3, E^rng/total}, {-0.5, 
        0}]}], Plot[{sigmoid@z}, {z, -rng, rng}, 
    PlotRange -> {{-rng, rng}, {0, prng}}, 
    PlotLegends -> 
     Placed[{Text[Style[{"Sigmoid"}, Italic, Orange, 7]]}, {{0.11, 
        0}, {0, -5.2}}], PlotStyle -> Orange], 
   Plot[{relu@z}, {z, -rng, rng}, 
    PlotRange -> {{-rng, rng}, {0, prng}}, 
    PlotLegends -> 
     Placed[{Text[Style[{"Relu"}, Italic, Purple, 7]]}, {{0.11, 
        0}, {0, -5}}], PlotStyle -> Purple], 
   Plot[{softplus@z}, {z, -rng, rng}, 
    PlotRange -> {{-rng, rng}, {0, prng}}, 
    PlotLegends -> 
     Placed[{Text[Style[{"SoftPlus"}, Italic, Red, 7]]} , {{0.11, 
        0}, {0, -4.7}}], PlotStyle -> Green]}, 
  FrameLabel -> {"z", "a(z)"}, AxesOrigin -> {-rng, 0}, Frame -> True,
   Prolog -> {GrayLevel[.95], Line[{{0, 0}, {0, prng}}], Dashed, 
    Line[{{-rng, 1.0}, {rng, 1.0}}]}, PlotRangeClipping -> False, 
  ImagePadding -> {{Automatic, 45}, {Automatic, Automatic}}], {{rng, 
   5, "Logit (z) range"}, 1, 
  10}, {{saturation, 1, "Logit concentration"}, 1, 1/n, 
  0.001},(*Activation,a(z),fraction in highest*)Delimiter, {{n, 10, 
   "Classes (has no effect)"}, 1, 50, 1, 
  Visible -> False}, {{prng, 1.1, "Activation (a) scale range"}, 1.0, 
  2*rng}]

enter image description here

Answered by Alucard on July 14, 2021

sigmoid[z_] := E^z/(E^z + 1)
relu[z_] := Max[{0, #}] & /@ z
softplus[z_] := Log[1 + E^z]

Manipulate[total = E^rng/saturation;
 Plot[{ConditionalExpression[E^z/total, -rng <= z <= Log[saturation total]], 
   sigmoid@z, relu@z, softplus@z}, {z, -rng, rng}, 
  PlotRange -> {{-rng, rng}, {0, prng}}, AxesOrigin -> {-rng, 0}, 
  Frame -> True, PlotStyle -> {Blue, Orange, Purple, Green}, ImageSize -> 500, 
  PlotRangeClipping -> False, PlotRangePadding -> Automatic, 
  ImagePadding -> {{Automatic, 45}, {Automatic, Automatic}}, 
  Epilog -> {Text[Style[N[E^rng/total, 4]], {rng + .3, E^rng/total}, {-0.5, 0}], 
    GrayLevel[.9], Line[{{0, 0}, {0, prng}}], Dashed, 
    Line[{{-rng, 1.0}, {rng, 1.0}}]}, FrameLabel -> {"z", "a(z)"}, 
  PlotLegends -> Placed[LineLegend[Automatic, 
    {StringForm["Softmax (`1`%)", NumberForm[saturation*100, {3, 1}]], 
    "Sigmoid", "ReLU", "Softplus"}, 
  "Spacings" -> {.5, .1}, LabelStyle -> 16], {Left, Top}]], 
 {{rng, 5, "Logit (z) range"}, 1, 10}, 
 {{saturation, 1, "Logit concentration"}, 1, 1/n, 0.001}, 
 Delimiter, 
 {{n, 10, "Classes (has no effect)"}, 1, 50, 1, Visible -> False},
 {{prng, 1.1, "Activation (a) scale range"}, 1.0, 2*rng}]

enter image description here

Note: You can use LegendLayout -> (Grid[##, Spacings -> {.5, .1}, Alignment -> Left] &) instead of "Spacings" -> {.5,.1} on macOS to avoid red syntax highlighting.

Answered by kglr on July 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP