TransWikia.com

Rewrite ListPlot using Graphics while perserving point color?

Mathematica Asked by Illari on December 15, 2020

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}}};

3 Answers

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 *)

Correct answer by MarcoB on December 15, 2020

You can also use VertexColors to style a list of points.

Using your coloredData as input:

pointListWithVertexColors = Point[#, VertexColors -> #2] & @@ 
    Transpose[coloredData /. Style -> List];

Graphics[pointListWithVertexColors, ImageSize -> Large]

enter image description here

Using data as input we can do:

pointListWithVertexColors2 = Point[#, VertexColors -> #2] & @@ 
  Transpose[{{#, #2},  {#3, #6} /.
    {{0, 0} -> Gray, {0, 1} -> Black, {1, 0} -> Red}} & @@@ (Join @@ data)]

pointListWithVertexColors == pointListWithVertexColors2
True

Answered by kglr on December 15, 2020

Another way, using the efficient GraphicsComplex:

Graphics[
 GraphicsComplex[
  Flatten[N@data[[All, All, {1, 2}]], 1],
  Point[
   Range@Length@data,
   VertexColors -> Flatten[
     data[[All, All, {3, 6}]] /. {{0, 0} -> Gray, {0, 1} -> Black, {1, 0} -> Red},
     1]]
  ]
 ]

Answered by Michael E2 on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP