0
$\begingroup$

I'm trying to have the color of each line show up in the plot legend for the following block of code below, except the corresponding colors for each line of the plot do not show up in the legend. I tried moving the PlotLegends call into "Show" and was also unsuccessful...

Da = {0, 0.01, 0.1, 1, 0.4, 0.5, 0.6, 1, 1.2, 1.5};
colorList = ColorData[3, "ColorList"]
gamma2 = 10;
Show[Plot[1 + ((Da[[#]]/(6*gamma2 + 6))*(nu^2 - 1)), {nu, 0, 1}, 
PlotStyle -> colorList[[#]], 
PlotLegends -> "Da=" <> ToString[Da[[#]]]] & /@ 
Range[Length[Da]],
PlotRange -> {{0, 1}, {0.97, 1}},
PlotLabel -> 
" \[Gamma]=10",
ImageSize -> Large]

as seen in the attached image below, the line colors aren't appearing in the legend. enter image description here

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Chris K
    Commented Nov 12, 2018 at 4:25

2 Answers 2

4
$\begingroup$

Your code can be simplified quite a bit: Rather than mapping you can take advantage of listability. So

1 + ((Da[[#]]/(6*gamma2 + 6))*(nu^2 - 1))& /@ Range[Length[Da]]

becomes

1 + ((Da/(6*gamma2 + 6))*(nu^2 - 1))

And also the plot does not require Show

Plot[Evaluate[1 + ((Da/(6*gamma2 + 6))*(nu^2 - 1))], {nu, 0, 1},
 PlotStyle -> ColorData[3, "ColorList"], 
 PlotLegends -> Placed["Da = " <> ToString[#] & /@ Da, Right],
 PlotLabel -> " \[Gamma]=10",
 ImageSize -> Large]

enter image description here

Note that it could be simplified further by using simply:

PlotStyle -> 3,

i.e. no need to explicitly mention ColorData

$\endgroup$
1
$\begingroup$

You aren't giving PlotLegends a way to reference your colors. Try something like this where I have created a Row that also references colorList:

Show[
 Plot[
    1 + ((Da[[#]]/(6*gamma2 + 6))*(nu^2 - 1)), {nu, 0, 1},
    PlotStyle -> colorList[[#]],
    PlotLegends -> Row[{
       Graphics[{colorList[[#]], Line[{{0, 0}, {.5, 0}}]}, 
        ImageSize -> {30, 10}, AspectRatio -> 1/10],
       Style[" Da = " <> ToString[Da[[#]]], "Text"]
       }]
    ] & /@ Range[Length[Da]],
 PlotRange -> {{0, 1}, {0.97, 1}},
 PlotLabel -> " \[Gamma]=10",
 ImageSize -> Large
 ]

enter image description here

$\endgroup$
2
  • $\begingroup$ thanks so much! this is really helpful. could you explain how to make the color a line? $\endgroup$ Commented Nov 12, 2018 at 2:02
  • $\begingroup$ @wiscoYogi added! $\endgroup$
    – ktm
    Commented Nov 12, 2018 at 2:05

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.