4
$\begingroup$

I try to plot several curves on one plot with legends, but the colors assigned to each curve are not showing in the legends.

Plot[
  u + (1 - u)*(1/Pi*ArcTan[# (u - 1)] + 1/2) - 
    (u + 1)*(1/Pi*ArcTan[-# (u + 1)] + 1/2), 
  {u, -3, 3}, 
  PlotRange -> 1.3, PlotStyle -> ColorData[91][#], 
  PlotLegends -> ToString[#]] & /@ {1, 2, 3, 10} // Show

enter image description here

$\endgroup$
5
$\begingroup$

The line in the legend does not appear for a single curve

Plot[Sin[x], {x, 0, 2 Pi}, PlotLegends -> "1"]

enter image description here

so the Map and Show construction will not display the lines either. To get those lines, you can wrap the value of the PlotLegends option in a List. For the above example, this yields

Plot[Sin[x], {x, 0, 2 Pi}, PlotLegends -> {"1"}]

enter image description here

and for your specific input

Plot[u + (1 - u)*(1/Pi*ArcTan[# (u - 1)] + 1/2) - (u + 1)*(1/Pi*ArcTan[-# (u + 1)] + 1/2), 
    {u, -3, 3}, 
    PlotRange -> 1.3, PlotStyle -> ColorData[91][#], PlotLegends -> {ToString[#]}
] & /@ {1, 2, 3, 10} // Show

enter image description here

Another approach that uses only one call to Plot could be:

val = {1, 2, 3, 10};
Plot[
  Evaluate[(u + (1 - u)*(1/Pi*ArcTan[# (u - 1)] + 1/2) - 
            (u + 1)*(1/Pi*ArcTan[-# (u + 1)] + 1/2)) & /@ val
  ], {u, -3, 3}, 
  PlotRange -> 1.3, PlotStyle -> ColorData[91] /@ val, PlotLegends -> ToString /@ val
]

enter image description here

The vertical spacings of the legends differ from those of the previous plot.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Perfect !! Thanks a lot for your very clear answer and your additional information for Plot ! $\endgroup$ – henry Oct 20 '16 at 15:27
  • $\begingroup$ Glad this was helpful :) $\endgroup$ – user31159 Oct 20 '16 at 15:30
  • 2
    $\begingroup$ @DoHe to be clear, PlotLegends accepts arbitrary expressions, so that when you pass in "1" it is displaying the expression you enter which is displayed according to its formatting rules. Wrapping it in a List triggers the more extensive legend processing, and it is why you get the line then. $\endgroup$ – rcollyer Oct 20 '16 at 16:06

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.