1
$\begingroup$

I have a lot of data points, and I have noticed that plotting using Graphics is usually much quicker than plotting with ListPlot. The problem is I am trying to color the data points based off some conditions, so for ListPlot, what I have written to do this is:

coloredData = Style[{#1, #2}, {#3, #6} /. {{0, 0} -> Gray, {0, 1} -> Black, {1, 0} -> Red}] & @@@ Partition[Flatten@data, 6]
ListPlot[coloredData]

enter image description here

Now, without the Style, if I were plotting my data using Graphics, I do:

dPlot = Table[Point[data[[i, All, {1, 2}]]], {i, Length@data}];

enter image description here

So I wanted to do the same thing with coloredData, and wrote:

cPlot = Table[Point[Partition[coloredData, 20][[i, All, {1, 2}]]], {i, Length@Partition[coloredData, 20]}];

enter image description here

From the error message it seems that now the data is in the wrong format for plotting using Graphics.

So my question is this: how do I rewrite what I currently plot using ListPlot, but with Graphics? When I try looking up phrases such as "coloring points according to conditions" I'm finding results like this and this which use ListPlot, and for computational time concerns, I'd like to use Graphics instead. Any advice for how to tackle this, would be much appreciated!

Here is some of my data:

data = {{{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913,0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446,0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266,0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297,1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234,0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913,0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446,0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266,0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234, 0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297,1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}, {{20, 6.48815, 1, 0.876608, 0.5, 0}, {20, 6.47738, 0, 0.521964, 0.5, 0}, {20, 5.97118, 0, 0.0862234,0.5, 0}, {20, 2.15164, 0, 0.377913, 0.5, 0}, {20, 6.43098, 0, 0.0116446, 0.5, 0}, {20, 7.88699, 1, 0.927266, 0.5, 0}, {20, 3.10361, 0, 0.543757, 0.5, 0}, {20, 7.96474, 1, 0.479332, 0.5, 0}, {20, 1.86771, 0, 0.245349, 0.5, 0}, {20, 7.12694, 1, 0.759896, 0.5, 0}, {20, 1.70262, 1, 0.984993, 0.5, 0}, {20, 5.54488, 1, 0.217045, 0.5, 0}, {20, 8.75599, 1, 0.459017, 0.5, 0}, {20, 2.24446, 1, 0.884729, 0.5, 0}, {20, 1.81927, 1, 0.583854, 0.5, 0}, {20, 2.45835, 0, 0.263973, 0.5, 0}, {20, 8.80958, 1, 0.91956, 0.5, 0}, {20, 2.96297, 1, 0.423835, 0.5, 0}, {20, 8.2311, 1, 0.98729, 0.5, 0}, {20, 5.76275, 1, 0.587943, 0.5, 0}}};
$\endgroup$
  • $\begingroup$ Try newcoloredData = Style[Point[{#1, #2}], <the rest of your code in the original coloredData>], then pass that to Graphics. Note the addition of Point inside Style. $\endgroup$ – MarcoB May 28 at 18:10
  • $\begingroup$ This comment worked like a charm! $\endgroup$ – Jomy Blue May 28 at 19:05
2
$\begingroup$

I would recommend generating a list of Point objects encapsulated in Style directives to feed to Graphics:

newcoloredData = 
  Style[
    Point[{#1, #2}], 
    {#3, #6} /. {{0, 0} -> Gray, {0, 1} -> Black, {1, 0} -> Red}
  ]& @@@ Flatten[data, 1];

Graphics[{coloredData}, Axes -> True]

graphics result

Note also that, rather than Flattening your data all the way, then re-Partitioning it, you can specify a level in Flatten to obtain the same result.


If you have to do the color conversion on a lot of points, the following might be faster than the ReplaceAll approach:

coloredData2 = 
  Style[
    Point[{#1, #2}], 
    {Gray, Black, Red}[[FromDigits[{#3, #6}, 2] + 1]]
  ]& @@@ Flatten[data, 1];

coloredData2 == newcoloredData         (* Out: True *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks a lot for your comment, it worked great! Really appreciate it $\endgroup$ – Jomy Blue May 28 at 19:06
  • $\begingroup$ @JomyBlue You are very welcome! $\endgroup$ – MarcoB May 28 at 19:25

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.