Message Boards Message Boards

How to color a ListLinePlot plot with derivative values?

Posted 11 years ago
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
ncurve={1,2,3,3,2,4,2,1}
I would like to color the lines between the mesh points using the "derivative" array
de={1,1,0,0,-1,2,-2,-1}
generated in the following way:
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
POSTED BY: jose muñoz
3 Replies
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:
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
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}]

POSTED BY: Vitaliy Kaurov
Posted 11 years ago
yes, i have made a mistake in the example, but the original question remains unanswered.
POSTED BY: jose muñoz
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:

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.
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract