2
$\begingroup$

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?

$\endgroup$
2

2 Answers 2

2
$\begingroup$
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]]

enter image description here

$\endgroup$
5
$\begingroup$

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
] 

Points colored by key

$\endgroup$

Your Answer

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

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