1
$\begingroup$

My objective is to use Plot with an arbitrary Color Scheme to get something like this

enter image description here

but I want to use any color scheme such as:

  1. a build-in scheme such as Rainbow
  2. or customize once such as

.

BarLegend[{{Darker@Red, Red, Lighter@Gray, Blue, Darker@Blue}, {1, 
   11}}, ColorFunctionScaling -> True]         

Here is my try:

dats = Table[n Sin[ x], {n, 1, 11}];
plot = Plot[dats, {x, -\[Pi], \[Pi]}, 
  ColorFunction -> ColorData["Rainbow"], Frame -> True, 
  ImagePadding -> 50, PlotRangeClipping -> False]     

enter image description here

Note that it is not working as desired, each plot should have the same color but it is coloring according to the y-axis.

For the customized color scheme mentioned above cant do it, any help, please?

$\endgroup$

2 Answers 2

5
$\begingroup$

Method-1

Use ParametricPlot and the tricks by @kglr (but I cann't find the original links now)

/. {(VertexColors -> None) -> VertexColors -> Automatic}
ParametricPlot[{x, n Sin[ x]}, {n, 1, 11}, {x, -π, π}, 
  AspectRatio -> 1, ColorFunction -> (ColorData["Rainbow"][#3] &), 
  MeshFunctions -> {#3 &}, Mesh -> 10, PlotPoints -> 50, 
  PlotStyle -> None, MeshStyle -> AbsoluteThickness[2], Axes -> False, 
  Frame -> False] /. {(VertexColors -> None) -> 
   VertexColors -> Automatic}

enter image description here

Method-2

Extract colors from ColorData and use PlotStyle.

Clear[dats, colors];
dats = Table[n Sin[x], {n, 1, 11}];
colors = ColorData["Rainbow"] /@ Subdivide[0, 1, 11]
plot = Plot[dats, {x, -π, π}, Frame -> True, 
  ImagePadding -> 50, PlotRangeClipping -> False, 
  PlotStyle -> colors]

enter image description here

$\endgroup$
2
  • $\begingroup$ I believe It should be a more elegant way to do that using plot $\endgroup$ Aug 15 at 8:12
  • $\begingroup$ Thanks a lot @cvgmt, That is EXACTLY what I was looking for $\endgroup$ Aug 15 at 8:29
3
$\begingroup$

Alternatively

Show[
Table[Plot[n Sin[x], {x, -\[Pi], \[Pi]},PlotStyle -> ColorData["Rainbow"][n/11]  ] , {n,1, 11}]
,PlotRange -> All]

enter image description here

$\endgroup$

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.