26
$\begingroup$

I am using Mathematica 8. In Mathematica, there is a default PlotStyle coloring in Plot and ListPlot. For example, suppose I have a series of nine functions stored in the list functions. Mathematica styles the different curves in a cyclic way (I think that the default is cycles of "blue", purple, dark yellow, green), as in this example:

functions = Table[a*Cos[x], {a, 0.2, 1, 0.1}]
Plot[functions, {x, 0, 2 Pi}]
{0.2 Cos[x], 0.3 Cos[x], 0.4 Cos[x], 0.5 Cos[x], 0.6 Cos[x], 0.7 Cos[x], 0.8 Cos[x],
 0.9 Cos[x], 1. Cos[x]}

plot1

This is nice, but now suppose that I want to color the multiple curves using some sort of gradient (perhaps one of the gradients in ColorData). Is this possible in Plot and ListPlot?

I could do this somewhat manually. For example, if I wanted a blue gradient, I could write the following (although this choice of colors could somehow be improved):

Plot[functions, {x, 0, 2 Pi}, PlotStyle -> {
   Lighter[Lighter[Lighter[Lighter[Blue]]]],
   Lighter[Lighter[Lighter[Blue]]],
   Lighter[Lighter[Blue]],
   Lighter[Blue],
   Blue,
   Darker[Blue],
   Darker[Darker[Blue]],
   Darker[Darker[Darker[Blue]]],
   Darker[Darker[Darker[Darker[Blue]]]]
   }]

plot2

However, this becomes complicated if I have many curves or if I want to use a more complex gradient such as rainbow (e.g., ColorData["Rainbow"]), temperature map (e.g., ColorData["TemperatureMap"]), etc. Do you have any suggestions?

$\endgroup$
2
  • 3
    $\begingroup$ Is this ok or You want more automatic option? Plot[Evaluate@Table[Sin[x] + t, {t, 0, 1, .1}], {x, 0, Pi}, PlotStyle -> Table[Blend[{White, Blue, Black}, i], {i, 0, 1, .1}]] Blend works also with palletes. Blend["Rainbow",x] $\endgroup$
    – Kuba
    Jun 27, 2013 at 20:47
  • $\begingroup$ @Kuba This is nice! Thank you! I just need to scale with respect to the number of curves, perhaps using Table[i/Length[functions], {i, 1, Length[functions]}], as halirutan does below. $\endgroup$
    – Andrew
    Jun 27, 2013 at 20:56

2 Answers 2

21
$\begingroup$

One pretty easy thing is to create a table of the gradient colors directly inside the Plot. The only thing you need to take care of is the scaling. All the color gradients take values between [0,1] when you access ColorData["GradientName",x]. Therefore, you need to now the number of your functions:

functions = Table[a*Cos[x], {a, 0.2, 1, 0.1}]
Plot[functions, {x, 0, 2 Pi}, 
 PlotStyle -> 
   Table[ColorData["Rainbow", i/(Length[functions]-1)], {i,0, Length[functions]-1}]
]

enter image description here

If you want a sneak preview for all color schemes, you can quickly hack some Manipulate

enter image description here

With[{
  control = (ColorData[#, "ColorFunction"] -> 
            Show[ColorData[#, "Image"], ImageSize -> 90]) & /@ ColorData["Gradients"],
  fns = Table[a*Cos[x], {a, 0.2, 1, 0.1}]},
  Manipulate[
   Plot[fns, {x, 0, 2 Pi},
    PlotStyle -> 
     Table[s@Rescale[c, {1, Length[fns]}, {cmin, cmax}], {c, 
       Length[fns]}]],
   {{s, control[[1, 1]], "Color Scheme"}, control},
   {{cmin, 0, "Color Min"}, 0, cmax},
   {{cmax, 1, "Color Max"}, .1, 1}
   ]
]
$\endgroup$
2
  • 1
    $\begingroup$ Thanks! This is just a thought... instead of using Table[ColorData["Rainbow", i/Length[functions]], {i, Length[functions]}], another possibility might be Table[ColorData["Rainbow", i], {i, 0, 1, 1/(Length[functions] - 1)}]. Whereas Table[i/Length[functions], {i, Length[functions]}] gives {1/9, 2/9, 1/3, 4/9, 5/9, 2/3, 7/9, 8/9, 1}, Table[i, {i, 0, 1, 1/(Length[functions] - 1)}] gives {0, 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, 1}. Thank you so much for your time and detailed and excellent help! $\endgroup$
    – Andrew
    Jun 27, 2013 at 21:15
  • $\begingroup$ @Andrew Hehe, yes you are right. I was about to edit the answer shortly, but I'm preparing something else I want to include ;-) $\endgroup$
    – halirutan
    Jun 27, 2013 at 21:17
1
$\begingroup$

In a modern versions You can use just PlotStyle->"Rainbow" or any other gradient or color list; gradient will be already scaled to all plots and color list will be looped if there is insufficient numbers of colors inside:

functions = Table[a*Cos[x], {a, 0.2, 1, 0.1}]
Table[Plot[functions, {x, 0, 2 Pi}, 
  PlotStyle -> i], {i, {"Rainbow", "BeachColors", "DarkBands","Pastel", ColorData[42]}}]

enter image description here

However, it is not stretched in case of Physical gradients since their range is different from {0,1}:

Table[Plot[functions, {x, 0, 2 Pi}, 
  PlotStyle -> i], {i, {"BlackBodySpectrum","VisibleSpectrum","HypsometricTints"}}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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