1
$\begingroup$

Consider the following plot where some individual curves have been given inline legends via Legended. A BarLegend is also added via PlotLegends. The problem is that the BarLegend hides some of the other legended items. How to place the BarLegend appropriately?

Incorrect plot hiding curve legends

enter image description here

Expected additional legends: enter image description here

rf = Function[{f, x}, f Sin[2 x] + 2 Cos[x] Sqrt[f (1 - f Cos[x]^2)]];

fMax[\[Theta]_] = 
 Piecewise[{{1/(    2 - 2 Sin[\[Theta]]), -(\[Pi]/2) < \[Theta] <= \[Pi]/
     6}, {1, \[Pi]/6 <= \[Theta] <= \[Pi]/2}}, 0];

thetaOpt[f_] = ArcTan[Sqrt[f]];

With[{n = 50},
 Show[{
   Plot[
    {
     Evaluate[
      MapAt[Legended[#, "\!\(\*SubscriptBox[\(R\), \(f = 1\)]\)"] &, 
       Table[Tooltip[rf[f, x], f], {f, 0, 1, 1/n}], -1]],
     Style[
      Legended[rf[fMax[x], x], 
       "\!\(\*SubscriptBox[\(R\), SubscriptBox[\(f\), \(max\)]]\)"], 
      Black, Dashed]
     },
    {x, -\[Pi]/2, \[Pi]/2},
    Ticks -> {Range[-10, 10] \[Pi]/8, Automatic},
    PlotStyle -> Hue /@ Range[1, 0, -1/n],

    PlotLegends -> BarLegend[{Hue, {0, 1}}, LegendLabel -> "f"]
    ],
   Plot[
    Legended[rf[Tan[x]^2, x], 
     "\!\(\*SubscriptBox[\(R\), SubscriptBox[\(\[Theta]\), \
\(optimum\)]]\)"],
    {x, 0, \[Pi]/2},
    PlotStyle -> {{Gray, Dashed}}
    ]

   },
  AxesLabel -> {"\[Theta]", "R/H"}
  ]
 ]

$\endgroup$
1
$\begingroup$

You can create two plots and combine them using Show:

n = 50;
plots1 = Plot[Evaluate @ Table[Tooltip[rf[f, x], f], {f, 0, 1, 1/n}], 
  {x, -π/2, π/2}, PlotStyle -> Hue /@ Range[1, 0, -1/n], 
   PlotLegends -> BarLegend[{Hue, {0, 1}}, LegendLabel -> "f"], 
   AxesLabel -> {"θ", "R/H"}, Ticks -> {Range[-10, 10] π/8, Automatic}];

plots2 = Plot[{Tooltip[rf[1, x], 1], rf[fMax[x], x], 
    ConditionalExpression[rf[Tan[x]^2, x], x >= 0]}, {x, -π/2, π/2}, 
   PlotStyle -> {Hue[0], Directive[ Black, Dashed], {Gray, Dashed}}, 
   PlotLegends -> (Subscript[R, #] & /@ 
    {Row[{f, 1}, "="], Subscript[f, max], Subscript[θ, optimum]})];

Show[plots1, plots2]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ thnx...works!....kind of a hack ;-)..no better way? $\endgroup$ – lineage May 8 at 12:46
  • $\begingroup$ @lineage, updated with an alternative (cleaner) method. $\endgroup$ – kglr May 8 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.