# ListPlot under conditions

I have a data file containing several columns and thousands lines of data. Below I present a small portion of it, in order to express my issue:

-0.54      0.09   -.3983969954588588E-02   0.2776322403476376E+00   0.5000000000000000E+05   0.1217096235496098E-13    0
-0.53     -0.03   -.3220837268589903E+00   0.7636149973371797E-02   0.1000000000000000E+04   0.7161538340416553E+00    0
-0.52      0.12   0.4319398001406393E+00   0.3046084462020560E-01   0.5000000000000000E+05   0.2227502533766236E-13    0
-0.51     -0.17   0.4596503785112276E+00   0.1788976667427210E+00   0.1000000000000000E+04   0.4171435790075386E+00    0
-0.12     -0.34   0.5787213420082925E+00   -.9322804977650871E+00   0.5910000000000000E+01   0.5147520415218694E+00    2
-0.12      0.13   0.5730427588243323E+00   0.9338351799657055E+00   0.1128500000000000E+03   0.7609604604284203E-06    1
-0.12      0.50   0.7396872499032718E+00   -.8091545263006905E+00   0.2137430000000000E+04   0.2962964572602038E-14    2
-0.09      0.05   0.6996863847044283E+00   0.8430535062986785E+00   0.2116400000000000E+03   0.3193612197737323E-08    1


The first two columns correspond to the coordinates $x$ and $y$, the next three columns are completely unrelated to this question, sixth column gives the value of a quantity $f$, while the last column shows a signal $s$, which can take only three integer values (0,1,2). What I want, is to plot the first two columns at the $(x,y)$ plane, using ListPlot but assign a specific color to each point (dot) according to the values of the last two columns. In particular, using FORTRAN notation, the conditions are:

IF(f.GT.1d-4.AND.s.EQ.0) ---> orange color
IF(f.LE.1d-4.AND.s.EQ.0) ---> red color
IF(f.GT.1d-4.AND.(s.EQ.1.OR.s.EQ.2) ---> green color
IF(f.LE.1d-4.AND.(s.EQ.1.OR.s.EQ.2) ---> blue color


I know, that I could write a simple FORTRAN code reading my data file and use the conditions to spit it into four different files. However, since this is only needed for plotting the data, I suppose that Mathematica can perform ListPlot under conditions, thus saving me disk space occupied by unnecessary extra files.

EDIT

Following @Nasser's approach and rearranging a little bit the colors, this is the final product • Shouldn't pac-man be yellow?
– Kuba
Jul 29, 2013 at 7:44
• @Kuba Right! I didn't thought of it! Jul 29, 2013 at 7:49
• @Vaggelis_Z would you mind uploading the full data file somewhere? I'd like to play with that a bit. Jul 29, 2013 at 8:42
• @Mr.Wizard Please see the EDIT, it contains a link for the complete data file. While playing with it, if you want, see if it possible to reduce the size of the dots but eliminating the white space between them. Jul 29, 2013 at 8:50
• I think "[FilledSmallSquare]" could be optimal plot marker, please take a look at my edit.
– Kuba
Jul 29, 2013 at 10:09

You mean something like this?

SetDirectory[NotebookDirectory[]];
m = Import["f.txt", "Table"];
getColor[m_List, i_Integer] := Module[{lim1 = 0.0001, f = m[[i, 6]], s = m[[i, 7]]},
Which[
f > lim1 && s == 0, Orange,
f <= lim1 && s == 0, Red,
f > lim1 && (s == 1 || s == 2), Green,
f <= lim1 && (s == 1 || s == 2), Blue,
True,Black (*just in case*)
]
];

data = Table[{PointSize[Large], getColor[m, i],
Point[{m[[i, 1]], m[[i, 2]]}]}, {i, 1, Length[m]}];

Graphics[data, Axes -> True, PlotRange -> All, Frame -> True,
GridLines -> Automatic, GridLinesStyle -> LightGray] >ls -lrt
total 15
-rwxrwxrwx 1 me me   974 Jul 29 00:24 f.txt
-rwxrwxrwx 1 me me 13375 Jul 29 00:39 f.nb
>cat f.txt
-0.54      0.09   -.3983969954588588E-02   0.2776322403476376E+00   0.5000000000000000E+05   0.1217096235496098E-13    0
-0.53     -0.03   -.3220837268589903E+00   0.7636149973371797E-02   0.1000000000000000E+04   0.7161538340416553E+00    0
-0.52      0.12   0.4319398001406393E+00   0.3046084462020560E-01   0.5000000000000000E+05   0.2227502533766236E-13    0
-0.51     -0.17   0.4596503785112276E+00   0.1788976667427210E+00   0.1000000000000000E+04   0.4171435790075386E+00    0
-0.12     -0.34   0.5787213420082925E+00   -.9322804977650871E+00   0.5910000000000000E+01   0.5147520415218694E+00    2
-0.12      0.13   0.5730427588243323E+00   0.9338351799657055E+00   0.1128500000000000E+03   0.7609604604284203E-06    1
-0.12      0.50   0.7396872499032718E+00   -.8091545263006905E+00   0.2137430000000000E+04   0.2962964572602038E-14    2
-0.09      0.05   0.6996863847044283E+00   0.8430535062986785E+00   0.2116400000000000E+03   0.3193612197737323E-08    1


Any graphics can be used. Here is one using Rectangle

SetDirectory[NotebookDirectory[]];
m = Import["f.txt", "Table"];
getColor[m_List, i_Integer] :=
Module[{lim1 = 0.0001, f = m[[i, 6]], s = m[[i, 7]]},
Which[f > lim1 && s == 0, Orange, f <= lim1 && s == 0, Red,
f > lim1 && (s == 1 || s == 2), Green,
f <= lim1 && (s == 1 || s == 2), Blue, True,
Black (*just in case*)]];

siz = 0.008; (*change as needed*)

data = Table[{getColor[m, i],
Rectangle[{m[[i, 1]] - siz, m[[i, 2]] - siz}, {m[[i, 1]] + siz,
m[[i, 2]] + siz}]}, {i, 1, Length[m]}];

Graphics[data, Axes -> True, PlotRange -> All, Frame -> True,
GridLines -> Automatic, GridLinesStyle -> LightGray] • Yes! This is exactly what I mean. Jul 29, 2013 at 7:24
• According to your approach, each point is represented by a filled color dot. Is it possible to change this using another plot marker, let's say square? Jul 29, 2013 at 11:35

Different approach:

list = Import["nb.txt", "Table"]

lim = .0001;
c1 = # > lim && #2 == 0 &;
c2 = # <= lim && #2 == 0 &;
c3 = # > lim && (MemberQ[{1, 2}, #2]) &;
c4 = # <= lim && (MemberQ[{1, 2}, #2]) &;


An array with indicators if given record fulfills given condition:

cond = Outer[Apply, {c1, c2, c3, c4}, list[[ ;; , {-2, -1}]], 1]

ListPlot[Pick[list[[ ;; , ;; 2]], #] & /@ cond,
PlotStyle -> {Orange, Red, Green, Blue},
BaseStyle -> Directive[AbsolutePointSize@10]] Edit

 ListPlot[Pick[list[[;; , ;; 2]], #] & /@ cond,
PlotStyle -> {Orange, Red, Green, Blue}, AspectRatio -> Automatic,
BaseStyle -> Directive[AbsolutePointSize@3],
PlotMarkers -> "\[FilledSmallSquare]"] It seems your data is an equally spaced array with dimensions:

Length /@ (Union /@ (list[[ ;; , {1, 2}]] // Transpose))

{109, 109}


I'm going to post the code, some explanations will appear this evening. This is my approach to plot colored plane with not points overlapping.

data = Table[, {109}, {109}];
colordata = Table[RGBColor[0, 0, 1], {109}, {109}];

g = Which[# > lim && #2 == 0, Orange,
# <= lim && #2 == 0, Red,
# > lim && (MemberQ[{1, 2}, #2]), Green,
# <= lim && (MemberQ[{1, 2}, #2]), Blue] &;

(data = ReplacePart[data, IntegerPart[(.55 + #) 100] -> 1]) & /@ list[[ ;; , {1, 2}]];

(colordata =
ReplacePart[colordata,
({110 - #2, #1} & @@ IntegerPart[(.55 + {#[[ 1]], #[[ 2]]}) 100]
) -> #[[ 3]]]
) & /@ ({#, #2, g[##3]} & @@@ list[[;; , {1, 2, -2, -1}]]);

ListPlot3D[data, VertexColors -> {colordata}, Mesh -> None, Lighting -> "Neutral",
ViewPoint -> {0, 0, 1000}, ImageSize -> 500, Axes -> False] If you are not concerned by the particular color order or choice, as your conditions partition your data you could use GatherBy[]. Let your data be in variable data. You could use:

gather=GatherBy[data,{#[[-2]]<=0.0001,#[[-1]]==0}&];
ListPlot[#[[All,{1,2}]]&/@gather,PlotStyle->{Orange,Red,Green,Blue}]


The gather will partition your data into the 4 possible outcomes sorted depending on how data is sorted/unsorted. Obviously you can adjust point size and other options at your discretion.

Using the dataset availablle for download the timing of GatherBy[] was 0.109201 seconds. Using:

ListPlot[#[[All, {1, 2}]] & /@ gather,
PlotStyle -> {Red, Orange, Blue, Green}, AspectRatio -> 1,
BaseStyle -> {PointSize[0.013], FontFamily -> "Kartika", 12},
Frame -> True, Axes -> False]


yields: Consistent with question edit (except for coloring and some style aspects)

Here's my approach:
The data is already quantized on a 1/100 grid so I believe we should be plotting it as such.

First Import the data and extract the columns we wish to work with:

data = Import["data_gRC.out", "Table"][[All, {1, 2, 6, 7}]];


Check the ranges of the coordinates:

minmax = {Min@#, Max@#} & /@ Take[Transpose[data], 2]

{{-0.54, 0.54}, {-0.54, 0.54}}


Build a color function (borrowing from Nasser):

fn[f_, 0] /; f <= lim1     = Red;
fn[_, 0]                   = Orange;
fn[f_, 1 | 2] /; f <= lim1 = Blue;
fn[_, 1 | 2]               = Green;
fn[__]                     = Black;

lim1 = 0.0001;


Build an array of colors from the coordinates and fn using SparseArray, first rounding and scaling the coordinates to integer values starting from one (here 55 + 100 x):

sa = SparseArray[Round[55 + 100 {#, #2}] -> fn[##3] & @@@ data];


Do the magic with MatrixPlot:

MatrixPlot[
Reverse[sa\[Transpose]],
DataRange -> minmax
] I might have understood the question in a wrong way, but I would use the ColorFunction Option of Listplot in Combination with a suitable anonymous function to generate the Color depending on the (x,y) coordinates.