1
$\begingroup$

I want to colour the points in a ListPlot with a list of colours, i.e. something like

data = Table[Cos[x]^2, {x, 0, 10, 0.1}];
colourData = Table[Cos[x]^2, {x, 0, 10, 0.1}];
colours = (ColorData["TemperatureMap"][#] &) /@ colourData;
ListPlot[data,Joined->True (* Something like PlotStyle -> colours*)]

And I want an output that looks like this

enter image description here

$\endgroup$
  • $\begingroup$ Your data can be generated as data = Cos[Range[0, 10, 0.1]] $\endgroup$ – OkkesDulgerci Sep 19 '19 at 14:58
1
$\begingroup$

Ok, I think I have something...

data = Table[Cos[x]^2, {x, 0, 10, 0.1}];
colourData = Table[Cos[x]^2, {x, 0, 10, 0.1}];
colours = (ColorData["TemperatureMap"][#] &) /@ colourData;
ListPlot[data, ColorFunction -> (Blend[colours, #] &), Joined -> True]

enter image description here

This can be used when the data and colourData are different

data = Table[Cos[x], {x, 0, 10, 0.1}];
colourData = Table[Cos[x]^2, {x, 0, 10, 0.1}];
colours = (ColorData["TemperatureMap"][#] &) /@ colourData;
ListPlot[data, ColorFunction -> (Blend[colours, #] &), Joined -> True] 

enter image description here

| improve this answer | |
$\endgroup$
0
$\begingroup$

This question is well covered in ListPlot's documentation on ColorFunction.

data = Table[Cos[x]^2, {x, 0, 10, 0.1}];
ListPlot[data, Joined -> True, ColorFunction -> "TemperatureMap"]

gets you

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks but this doesn't answer the question. This method only allows us to use the y values as data for the colour function. Not an arbitrary function, or an arbitrary list of colours. $\endgroup$ – Tom Sep 20 '19 at 9:57
0
$\begingroup$

Since data and colours have the length, you can also post-process ListPlot output to inject VertexColors -> colours into the Line object:

data1 = Table[Cos[x]^2, {x, 0, 10, 0.1}];
data2 = Table[Cos[x], {x, 0, 10, 0.1}];
colourData = Table[Cos[x]^2, {x, 0, 10, 0.1}];
colours = (ColorData["TemperatureMap"][#] &) /@ colourData;

ListPlot[data1, Joined -> True] /. Line[x_] :> Line[x, VertexColors -> colours]

enter image description here

ListPlot[data2, Joined -> True] /. Line[x_] :> Line[x, VertexColors -> colours]

enter image description here

| improve this answer | |
$\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.