3
$\begingroup$

I am writing a small report, wherein I have several plots, some plots are on a linear-linear scale, some are in log-linear scale, some in log-log. The problem I am facing is every time I change my scale it is becoming very difficult for me to adjust the legend. right now I am customized my legend using line command and circle command of graphics. Is there any way to fix this suppose my plot image size is 1500 (which is fixed for all of my plots), with respect to this can I set my legends.example. I don't have a

style1 = PlotStyle -> {Black, Thickness[0.004], 
   AbsoluteDashing[{2, 7}]}
style2 = PlotStyle -> {Black, Thickness[0.004], 
   AbsoluteDashing[{8, 20}]}
style3 = PlotStyle -> {Black, Thickness[0.004], 
   AbsoluteDashing[{4, 18, 40}]}
p1 = Plot[Sin[π*x], {x, 0, 1}, Evaluate@style1];
p2 = Plot[Sin[2 π*x], {x, 0, 1}, Evaluate@style2];
p3 = Plot[Sin[3 π*x], {x, 0, 1}, Evaluate@style3];


s1 = Graphics[{Thickness[0.004], AbsoluteDashing[{2, 7}], 
    Line[{{0, 14}, {1.5, 14}}], 
    Text[StyleForm["\!\(\*SubscriptBox[\(β\), \(1\)]\),", 
      FontSize -> 30], {2.5, 14}]}];
s2 = Graphics[{Thickness[0.004], AbsoluteDashing[{8, 20}], 
    Line[{{3.5, 14}, {5, 14}}], 
    Text[StyleForm["\!\(\*SubscriptBox[\(β\), \(2\)]\),", 
      FontSize -> 30], {6, 14}]}];
s3 = Graphics[{Thickness[0.004], AbsoluteDashing[{4, 18, 40}], 
    Line[{{7, 14}, {10, 14}}], 
    Text[StyleForm["\!\(\*SubscriptBox[\(β\), \(3\)]\),", 
      FontSize -> 30], {10.5, 14}]}];

Show[p1, p2, p3,s1,s2,s3, PlotRange -> All]

The above plot is on a linear-linear scale I looking trying to fix the general legend for all type of scale.

$\endgroup$
3
  • $\begingroup$ Please add the code you're using to generate the legend. Otherwise, it is impossible for anyone to tell what's the problem with your legend $\endgroup$
    – Lukas Lang
    Commented Sep 30, 2019 at 14:42
  • $\begingroup$ Can we use the above code as a reference code, because it is pretty big. $\endgroup$
    – acoustics
    Commented Sep 30, 2019 at 14:46
  • 1
    $\begingroup$ I'm not asking for the full code with actual data (in fact, the example you have is much better) - but since your example image has a legend, and you are asking about how to position legends, it would help a lot if you could add the code you're currently using to create and position the legend. Even better would be if you could add the legend to the example above, and add a second example with e.g. log-log scaling and a legend that shows how the legend is no longer working in that case $\endgroup$
    – Lukas Lang
    Commented Sep 30, 2019 at 14:50

1 Answer 1

5
$\begingroup$

Create a legend using your style1, style2, style3:

legend = LineLegend[Directive[#2] & @@@ {style1, style2, style3}, 
      Subscript[β, #] & /@ Range[3], LegendLayout -> {"Row", 1}, LegendMarkerSize -> 60];

and use it as Epilog in Show controlling the position with the second argument of Inset:

Show[p1, p2, p3, PlotRange -> All, PlotRangePadding -> Scaled[.07],
  Epilog -> Inset[legend, {.5, 1.1}]]

enter image description here

or place it Above the plot using Legended:

Legended[Show[p1, p2, p3, PlotRange -> All], Placed[legend, Above]]

enter image description here

$\endgroup$
7
  • $\begingroup$ Does it work for Log-Linear scale, because last time I am trying to put , It was not adjusted properly. $\endgroup$
    – acoustics
    Commented Sep 30, 2019 at 16:35
  • $\begingroup$ @acoustics, both should work. Try, for example,Legended[LogLinearPlot[Tanh[x], {x, 1, 100}], Placed[legend, {.5, 1}]]. For the first approach, you can use LogLinearPlot[Tanh[x], {x, 1, 100}, Epilog -> Inset[legend, Scaled@{.5, 1}]]. $\endgroup$
    – kglr
    Commented Sep 30, 2019 at 17:12
  • $\begingroup$ I have tried modifying the code little bit by introducing a new legend circle, but I am not getting exactly what I wanted to do. Instead I am getting a straight line $\endgroup$
    – acoustics
    Commented Oct 1, 2019 at 9:00
  • $\begingroup$ style1 = PlotStyle -> {Black, Thickness[0.004], AbsoluteDashing[{2, 7}]} style2 = PlotStyle -> {Black, Thickness[0.004], AbsoluteDashing[{8, 20}]} style3 = PlotStyle -> {Black, Graphics[{Circle[]}, ImageSize -> 25]};legend = LineLegend[ Directive[#2] & @@@ {style1, style2, style3}, {Text[ Style["\!\(\*SubscriptBox[\(\[Beta]\), \(S\)]\),", 30]], Text[Style["\!\(\*SubscriptBox[\(\[Beta]\), \(AS\)]\)", 30]], Text[Style["FEM", 30]]}, LegendLayout -> {"Row", 1}, LegendMarkerSize -> 60] $\endgroup$
    – acoustics
    Commented Oct 1, 2019 at 9:01
  • 1
    $\begingroup$ @acoustics, change style3=... to style3 = PlotStyle -> {"LineColor" -> None}; $\endgroup$
    – kglr
    Commented Oct 1, 2019 at 10:01

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.