Variable color in curves

The difficulty

Color assignments have to be made to individual graphics primitives and most curves in plots are created by using the graphics primitive Line on a number of points.  This can be seen by looking at the form of a plot.

plot1  =  Plot[Sin[x], {x, 0, 2 π}]

If we look at the first Part of this plot we see that it is indeed a line.  Thus, if we assign a color to the plot it will apply to the entire line.

Short[plot1[[1]], 3]

Changing the color

For lines

The first thing we need to do if we want the color of the line to continuously change is to extract the line or lines from our plot.  We can do this with Cases.  Notice the underscore before "Line" and the infinity in the third position allows the function to work at all levels of the plot.  I am using the Short form for the output so make it clear that we have indeed found the lines in our plot.  Notice that the name assigned to the lines is inside the Short command so that we can work with the line.

Short[myLines  = Cases[plot1, _Line, Infinity], 3]

Next we are going to take the points from the line(s) and form new line segments which can have individual colors assigned to them.  (I will assume here that there is more than one Line in the plot to make the process general.)

segPoints  =  
        Map[Partition[First[#], 2, 1] &, myLines];

We can see the form of the elements of segPoints is a pair of points.  Notice that this pair of points is at level two of the list.

segPoints[[1, 3]]

Now we can assign colors to each of these pieces.  I will do this using the color function Hue.  First we generate a some values which will make the process easier.  We will use these values in assigning color values to the segments.  Notice the use of Apply and Flatten to get the argument of the Hue function to be of the correct form.

dims  =  Dimensions[segPoints]

colorSegments  = 
        Table[ Map[{Apply[Hue, 
                  Flatten[Position[segPoints[[i]], #]/N[dims[[2]]]]], 
                Line[#]} &, segPoints[[i]]], {i, 
            dims[[1]]}];

We can check to see that the form of the objects we have created is correct by looking at a single part.

colorSegments[[1, 5]]

For points

If we want to create scatter plots of the points rather than lines the task is even easier.  First we get the points.

pts  =  Map[First, myLines];

Again we will find it advantageous to have the dimensions of our list.

ptdims  =  Dimensions[pts]

Next we assign colors to the points.

colorPoints  = 
        Table[ Map[{Apply[Hue, 
                  Flatten[Position[pts[[i]], #]/N[ptdims[[2]]]]], 
                Point[#]} &, pts[[i]]], {i, 
            ptdims[[1]]}];

Now the plots

For the lines

To make use of what we have created we need to display our line segments using show.  To get the axes and other features of the original plot we need to add some options.

plot2  =  
      Show[Graphics[colorSegments], Axes  ->  True]

Show[GraphicsArray[{{plot1, plot2}}], 
      ImageSize  ->  580]

For the points

In this case you will note that there is a bunching of the points where the curvature is greater.  This is built into the plot function.  The only way to avoid this is to create your points using the table function, where you use a form of your function to allow "equal" spacing of the points.  By default all of the plot functions use more points in the vicinity of greater curvature.

plot3  =  
      Show[Graphics[colorPoints], Axes  ->  True]

The process

The above process can be used for all of the plot functions which generate plots using the graphics primitive Line.  So it should work with Plot, ParametricPlot, ParametricPlot3D, Graphics[ContourPlot] and ImplicitPlot.