4
$\begingroup$

How do I obtain only one Legend in the following case? I would like to obtain only one "a" and one "b" legend on the side, with two different colors. This means that I want a colored legend for 10 z^i group and one for 10i*z group.

Show[Flatten[{Table[
Plot[{10 z^i, 10 i*z}, {z, -1, 1}, PlotRange -> All, 
 PlotLegends -> {"a", "b"}], {i, 0, 5, 1}]}]]

enter image description here

What I would like is:

enter image description here

I have tried the Option:

  If[i == 1, PlotLegends -> {"a", "b"}, Unevaluated[Sequence[]]]

but it doesn't work

$\endgroup$
3
  • $\begingroup$ you could use Legended but I am not sure if this is what you are looking for; you seem to have six pairs of (parametric) functions you plot over the range {-1,1}; perhaps you would consider plotting each pair separately with one legend per plot if the labels 'a' and 'b' are not carrying some information or you could eg plot them all together using the specific values of i that generate each function in the legend $\endgroup$
    – user42582
    Commented Nov 28, 2017 at 13:18
  • $\begingroup$ @user42582 maybe the edit is more clear? $\endgroup$
    – Andrea G
    Commented Nov 28, 2017 at 13:39
  • 1
    $\begingroup$ @AndreaG take a look now at my answer. I think I got the picture. $\endgroup$ Commented Nov 28, 2017 at 14:12

2 Answers 2

6
$\begingroup$

Using Evaluate in Plot with a value for PlotStyle

 Plot[
   Evaluate@Table[{10 z^i, 10 i*z}, {i, 0, 5, 1}], 
   {z, -1, 1}, 
   PlotStyle -> Take[ColorData[68, "ColorList"], 2], 
   PlotLegends -> {"a", "b"}
  ]

seems to produce the requested effect

enter image description here

In the documentation for Plot there are explicit use cases for plotting several functions, together.

In this situation, I am not perfectly sure why the proposed solution works since we are dealing with pairs of functions (the input to Plot is a matrix-a list of lists).

Nevertheless, pairing Evaluate with an explicit option for PlotStyle works :)

Since the pair of colors provided in PlotStyle is cycled over the available input functions, it is not implausible to assume that Plot treats the lsit of pairs of functions as a flattened list of functions.

$\endgroup$
2
  • $\begingroup$ imagine now I wanted to use Filling to get filled region between each curve and a corresponding line at a height i (which then depends on the iterator i); how would I combine Filling with this requirement, now that Plot is before Table and iteration has been closed? $\endgroup$
    – Andrea G
    Commented Nov 28, 2017 at 14:54
  • 1
    $\begingroup$ I don't know if this helps, but if you evaluate funcs = Table[{10 z^i, i, 10 i*z, i}, {i, 0, 5, 1}] and then use Table[Plot[Evaluate@funcs[[i + 1]], {z, -1, 1}, PlotStyle -> {Red, Green}, Filling -> {1 -> {i, Directive[Orange, Opacity[0.3]]}, 3 -> {i, Directive[Orange, Opacity[0.3]]}}], {i, 0, 5, 1}], perhaps you'll get what you're looking for; using Filling in the original multi-plot gets things too crowded to make out any detail $\endgroup$
    – user42582
    Commented Nov 28, 2017 at 15:49
5
$\begingroup$

Something like this?:

Plot[{10 z^# & /@ Range[0, 5], 10 #*z & /@ Range[0, 5]}, {z, -1, 1}, 
PlotRange -> All, PlotLegends -> {a, b}]

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.