# How to color a ListLinePlot plot with derivative values?

**URL:** <https://community.wolfram.com/t/how-to-color-a-listlineplot-plot-with-derivative-values/521>\
**Category:** Questions\
**Tags:** data-science, mathematica, graphics-and-visualization, numerical-computation\
**Created:** [September 10, 2013, 4:59am UTC](https://community.wolfram.com/t/how-to-color-a-listlineplot-plot-with-derivative-values/521 "2013-09-10T04:59:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jomunozf](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jomunozf](https://community.wolfram.com/u/jomunozf)\
**Post date:** [September 10, 2013, 4:59am UTC](https://community.wolfram.com/t/how-to-color-a-listlineplot-plot-with-derivative-values/521/1 "2013-09-10T04:59:42Z")

</div>

I have an array of dimensions {n,M}, and for every nth curve generated, I want to color it using the “discrete” derivative information, that is to say, by using the slope of this linear-piecewise curve. For example, if the nth curve is

```auto
ncurve={1,2,3,3,2,4,2,1}

```

I would like to color the lines between the mesh points using the “derivative” array

```auto
de={1,1,0,0,-1,2,-2,-1}

```

generated in the following way:

```auto
For[i=1,i<=7,i++,de[[i]]=ncurve[[i+1]]-ncurve[[i]]];[/i][/i]

```

_\*This process of coloring can be summarized in order to plot the entire array in one drawing?_

 

_Thanks\*_

---

<div class="post-metadata">

**Author:** ![nma](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@nma](https://community.wolfram.com/u/nma)\
**Post date:** [September 10, 2013, 5:26am UTC](https://community.wolfram.com/t/how-to-color-a-listlineplot-plot-with-derivative-values/521/2 "2013-09-10T05:26:38Z")

</div>

If you show more data (ncurves) that will help with some MWE.  
But for your derivatives, what you show does not look correct. Mathematica has a function for this that you should be using:

```auto
ncurve = {1, 2, 3, 3, 2, 4, 2, 1};
Differences[ncurve]
(* {1, 1, 0, -1, 2, -2, -1} *)

```

The above is different from what you have in your question. One should get a differences list with length that is one less than the original list.

---

<div class="post-metadata">

**Author:** ![jomunozf](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@jomunozf](https://community.wolfram.com/u/jomunozf)\
**Post date:** [September 10, 2013, 5:56am UTC](https://community.wolfram.com/t/how-to-color-a-listlineplot-plot-with-derivative-values/521/3 "2013-09-10T05:56:32Z")

</div>

yes, i have made a mistake in the example, but the original question remains unanswered.

---

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [September 10, 2013, 7:06am UTC](https://community.wolfram.com/t/how-to-color-a-listlineplot-plot-with-derivative-values/521/4 "2013-09-10T07:06:43Z")

</div>

Please notice how Mathematica code is formatted on this site and follow this formatting.

About your question. I am not sure why are you complicating things - first let’s figure out how to do it for a single data set. Then you can generalize for all curves. Define a function:

```auto
f[data_] := {Thickness[.01], Opacity[.9], CapForm["Round"], ColorData["Rainbow"][#2], Line[#1]} & @@@ 
  Transpose[{Partition[data, 2, 1], Rescale[Differences[data[[All, 2]]]]}]

```

Now visualize. Note I am adding noise to show the effect changing of slope has on color

```auto
Manipulate[
 SeedRandom[1];
 data = Table[{x, 2 Sin[x] + RandomReal[h]}, {x, 0, 4 Pi, .1}];
 Graphics[f[data], Frame -> True, ImageSize -> 800, AspectRatio -> 1/4]
 , {h, 0, 1}]

```

![6717spin.gif](https://global.discourse-cdn.com/wolfram/original/3X/f/9/f97a496efaee510a946964dd264c8754937b086c.gif)
