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?
-
1$\begingroup$ Is Coloring Points in a Plot, based on their position in the list, Coloring Points in a List Plot of some help? $\endgroup$– user9660Apr 20, 2015 at 16:09
-
$\begingroup$ @Lou I looked at these, it was not obvious how to adapt them to my problem. In my problem the color of the point in the plotted list is controlled by a boolean value compute based on an element in a second list. $\endgroup$– WintermuteApr 20, 2015 at 16:21
2 Answers
list = {1, 2, 3, 4} ; keys = {7, 8, 9, 10};
styleddata = Style[#, {Black, Red}[[Mod[#2, 2, 1]]]] & @@@ Transpose[{list, keys}];
Or
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]]
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*)
ListPlot[
Transpose@{pts},
PlotStyle -> colors /@ keys
]