1
$\begingroup$

I have a list of pairs of functions:

fns = Table[{a*x, a*x^2}, {a, 5}]

{{x, x^2}, {2 x, 2 x^2}, {3 x, 3 x^2}, {4 x, 4 x^2}, {5 x, 5 x^2}}

What I would like to do is plot it so that the two functions in each of those pairs is the same color, but the first is solid and the second is dashed, and then have each pair be a different color than the others. So for example, it might be {{{Blue, Thick},{Blue, Dashed}},{{Red, Thick},{Red, Dashed}},...}.

If I simply do Plot[fns, {x, 0, 6}, PlotStyle -> {Thick, Dashed}], it does make the first in each pair solid and the second dashed, but it also makes them different colors:

enter image description here

I know I could solve this by relying on the fact that they will be colored in the same order in different plots, and separating the first and second elements of each pair and plotting them separately:

p1 = Plot[Evaluate@fns[[All, 1]], {x, 0, 6}, PlotStyle -> Thick];
p2 = Plot[Evaluate@fns[[All, 2]], {x, 0, 6}, PlotStyle -> Dashed];
Show[p2, p1]

enter image description here

But this is messy, and you can actually already see that I had to use Show[] in the order p2, p1, because if I do it as p1, p2, it uses p1's smaller vertical range, which makes it different than just plotting them all together.

I know there must be a simple way but I've read the documentation for PlotStyle and searched for posts here and can't find it. How can this be done?

$\endgroup$
  • 1
    $\begingroup$ I'm thinking: no. If it's any consolation, you can add , PlotRange -> All to the Show[] $\endgroup$ – Feyre Jul 21 '16 at 15:44
  • $\begingroup$ @Feyre I can think of another way, basically constructing a list manually beforehand for PlotStyle, but it's also inelegant. That's good to know though, thanks. $\endgroup$ – YungHummmma Jul 21 '16 at 16:07
  • 1
    $\begingroup$ I think your solution, with @Feyre's PlotRange recommendation, is probably the most elegant solution to this problem. You could localize p1 and p2 if you don't like having the extra global variables. Other than that, it seems short & sweet. $\endgroup$ – Michael E2 Jul 21 '16 at 19:25
4
$\begingroup$

Update

fns = Table[{a*x, a*x^2}, {a, 5}];

cd = Flatten@Table[{
     Directive[ColorData[97, c], Thick],
     Directive[ColorData[97, c], Dashed]
     }, {c, Length[fns]}];

Plot[Evaluate@fns, {x, 0, 6}, PlotStyle -> cd, PlotRange -> All, PlotLabels -> Automatic]

enter image description here


fns = Table[{a*x, a*x^2}, {a, 5}];

psA = Table[Directive[ColorData[97, c], Thick], {c, 5}];
psB = Table[Directive[ColorData[97, c], Dashed], {c, 5}];

Plot[{Evaluate@fns[[All, 1]], Evaluate@fns[[All, 2]]}, {x, 0, 6}, 
 PlotStyle -> Join[psA, psB], PlotLabels -> Automatic, 
 PlotRange -> All]

enter image description here

$\endgroup$
  • $\begingroup$ ...or, use ColorData[1, c] (pre-version 10) or ColorData[97, c] (current version) to get the default colors. $\endgroup$ – J. M. is away Jul 21 '16 at 17:09
  • $\begingroup$ @YungHummmma does this work for you? $\endgroup$ – Young Jul 22 '16 at 2:07

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.