2
$\begingroup$

I have a data set of type (x,y,E), where E can assume 4 distinct configurations (0,0), (1,0), (0,1) or (1,1). I want to create a 2D plot using ListPlot considering the rules:

  1. If E=(0,0), then print red
  2. If E=(1,0), then print blue
  3. If E=(0,1), then print green
  4. If E=(1,1), then print black the respective coordinate (x,y). See illustrative Figure below

enter image description here

data=Uncompress[FromCharacterCode[
  Flatten[ImageData[Import["https://i.stack.imgur.com/hQ9L8.png"], "Byte"]]]]

The code doesn't work

ListPlot[Partition[data[[All ;; 2]], 2, 1], 
 PlotStyle -> (Rest[
     data[[All, -1]]] /. {{0, 1} -> Green, {1, 0} -> Blue, {0, 0} -> 
      Red, {1, 1} -> Black})]

Can anybody help me?

$\endgroup$
2
$\begingroup$
colorRule = Association[{{0, 0} -> Red, {1, 0} -> Blue, {0, 1} -> 
     Green, {1, 1} -> Black}];

Wrap each xy pair with List and map colorRule on the last column to get a list of colors to be used as PlotStyle option setting:

colors = (colorRule /@ data[[All, -1]]);

ListPlot[List /@ data[[All, ;; 2]], 
 PlotStyle -> colors, 
 BaseStyle -> PointSize[Large]]

enter image description here

Alternatively, use Style to assign the desired colors to the xy pairs::

styledData = Style[{#, #2}, colorRule @ #3] & @@@ data;

ListPlot[styledData, BaseStyle -> PointSize[Large]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ A much faster version might be Graphics[Point[data[[All, ;; 2]], VertexColors -> colors], BaseStyle -> PointSize[Large]] $\endgroup$ – Ulrich Neumann Jun 30 '20 at 7:05
1
$\begingroup$

In addition to providing colors for each point individually as in kglr's answer, you can also group the elements by the last element before plotting. This can be done in several different ways. Here's one example:

groups = GroupBy[data, Last][[All, All, ;; 2]];
ListPlot[{
  groups[{0, 1}],
  groups[{1, 0}],
  groups[{0, 0}],
  groups[{1, 1}]
  },
 PlotStyle -> {Green, Blue, Red, Black},
 BaseStyle -> PointSize[Large]
 ]

List plot

$\endgroup$

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.