2
$\begingroup$

As the title states, I'd like to ListPlot a list of Associations, with each pair in Association (i.e. a point in the plot) a different color.

Example set:

{<|9.59201 -> 1.77876, 7.90731 -> -0.196118, 10.1576 -> 0.914597, 
  8.35059 -> 1.54213, 8.61681 -> -0.32721, 8.61687 -> 0.841388, 
  8.71977 -> 0.775458, 8.04974 -> -0.05546|>, <|11.3369 -> 1.71693, 
  10.2098 -> 1.12961, 9.50776 -> 0.686906, 9.73273 -> 0.0918487, 
  8.63463 -> -0.551922, 9.69208 -> 0.0742091, 8.9928 -> 0.246726, 
  7.99068 -> 0.74243|>, <|30.1425 -> 7.22639, 27.3945 -> 2.77409, 
  33.4071 -> 13.1696, 29.2098 -> 2.26177, 27.0366 -> 1.54904, 
  29.9498 -> 0.379177, 25.5956 -> -0.373022, 
  29.3194 -> 4.75372|>, <|21.4029 -> 1.84701, 21.7417 -> -1.72672, 
  23.5003 -> -4.96554, 21.8775 -> -4.55523, 24.4923 -> -2.542, 
  22.1722 -> 3.32604, 26.3751 -> -0.232237, 
  22.2524 -> -1.3161|>, <|23.7269 -> 5.57226, 22.3803 -> 0.569165, 
  26.5645 -> 0.623679, 21.6979 -> -0.707441, 17.283 -> -2.45536, 
  20.3572 -> 1.23667, 20.9486 -> -2.83034, 
  21.0028 -> 1.51677|>, <|19.7322 -> -0.364163, 19.0351 -> -0.368039, 
  19.3306 -> -4.58427, 20.5308 -> -3.3369, 19.3743 -> 0.0137566, 
  25.7891 -> 13.2349, 18.5857 -> 1.42911, 
  19.0113 -> -0.679348|>, <|28.1422 -> 10.7682, 24.0284 -> 2.00734, 
  30.2609 -> 3.3972, 27.7815 -> 7.30809, 22.3967 -> 3.78583, 
  27.2537 -> -4.45777, 22.1055 -> -2.03305, 25.1709 -> 5.63689|>}
$\endgroup$
  • 2
    $\begingroup$ Each association has different color or each point? With 56 points there it will be hard to get distinguishable colors. p.s. have you tried anything? $\endgroup$ – Kuba Jan 25 '16 at 10:32
  • 1
    $\begingroup$ That's my question too. Can you explain in clearer terms how the colour of each point will be determined? $\endgroup$ – Szabolcs Jan 25 '16 at 10:32
  • $\begingroup$ Ah yes, sorry, Indeed, 7 separate plots of 8 points. I tried Table[ ListPlot[data[[r]],PlotStyle ->{#, PointSize[0.02]} & /@ (Hue[#, 0.5, 1] & /@ (Range[8]/8))] ,{r,7}] but I can't manage to get them colored separately $\endgroup$ – user18798 Jan 25 '16 at 10:32
  • 1
    $\begingroup$ So in each of those 7 separate plots, each point has a different color? $\endgroup$ – Kuba Jan 25 '16 at 10:34
  • $\begingroup$ Yup, each first key-value pair should have the same color across plots $\endgroup$ – user18798 Jan 25 '16 at 10:35
2
$\begingroup$

We have to convert associations, e.g: <|1->2, 3->4|> to something like { { {1,2} }, {{3,4}}} so, not to the lists of pairs but lists of list with one element which is a pair. Then ListPlot will take those elements as separate plots.

ListPlot[
    #, BaseStyle -> AbsolutePointSize@15
] & /@ Apply[ List @* List, Normal[assoList], {-2}]

enter image description here

$\endgroup$
  • $\begingroup$ That's too bad, I had hoped to be able to take advantage of the Association structure. whatever works, works, though so thanks anyway. If you don't mind, I'll wait before accepting $\endgroup$ – user18798 Jan 25 '16 at 10:43
  • 1
    $\begingroup$ @user18798 I don't know what do you mean ;) p.s. it's not safe to keep coordinates in Association take a look at <|1->2, 2->5, 1->5|>, it will merge duplicates which is not always something one would want. $\endgroup$ – Kuba Jan 25 '16 at 10:45
  • $\begingroup$ True that. With e.g. labeling etc. I find it easier to just use Keys, and in my particular application, duplicate keys are non-existent :) but good to be informed about it in general $\endgroup$ – user18798 Jan 25 '16 at 10:56
  • $\begingroup$ @user18798 You can use similar transformation inside ListPlot, mapping over list of associations so you can still refer to keys and values in ListPlot. $\endgroup$ – Kuba Jan 25 '16 at 10:58
  • 1
    $\begingroup$ @user18798 yes, without one List we would have a list of pairs, and we want another list on each of them to make ListPlot thinking they are separate sets. $\endgroup$ – Kuba Jan 25 '16 at 11:16

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.