I have a collection of points I want to plot using ListPlot[]. I would like each individual point to be a certain color depending on a key value that is based on a list separate from the points. As a simple example suppose we have list={1,2,3,4} and keys={7,8,9,10}. If keys[[i]] is odd we want list[[i]] to be black, if keys[[i]] is even we want list[[i]] to be red. How can I do this?


2 Answers 2

list = {1, 2, 3, 4} ; keys = {7, 8, 9, 10};
styleddata = Style[#, {Black, Red}[[Mod[#2, 2, 1]]]] & @@@ Transpose[{list, keys}];


styleddata = Style[#, If[OddQ@#2, Black, Red]] & @@@ Transpose[{list, keys}];
styleddata = Style[#, #2 Black + (1 - #2) Red] & @@@Transpose[{list, Boole@OddQ@keys}];

ListPlot[styleddata, BaseStyle -> PointSize[Large]]

enter image description here


Just use a color function that you can map over your keys

keyList = {1, 2, 3};
keys = RandomChoice[keyList, 10];(*Dummy keys*)
pts = RandomInteger[100, {Length@keys, 2}];(*Dummy Points*)
colors[key_] := Hue[key/Length@keyList];(*A color function that you can modify*)

  PlotStyle -> colors /@ keys

Points colored by key


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.