# Colour coding a ListPlot from a 3rd string value

I'm trying to format a csv file to give a plot with x and y coordinates, but colour coded by a third value that is a string. I have no issue reading the data in and can plot the x and y coordinates. But I am a little stuck on how to colour code the plot from the strings.

E.g. of the data [2.3, 3.4, phot, 4.1, 5.2, compt]

So far I've read the data in and plotted it. My code so far is: AlNP20 = Import["file.csv", {"Data", 1 ;; 271, {1, 2}}];

AlNPProcess20 = Import["file.csv", {"Data", 1 ;; 271, {11}}];

ListPlot[AlNP20]

This gives me the plot of x and y, but any help on how to incorporate the strings as colour coding would be greatly appreciated!

• Can you give more information? What is "phot" and "compt"? Which strings are possible and how do they correspond to colors? Your example data has 6 columns but your import statement takes in the 11th column which is confusing. It would be ideal if you could give a minimal example (maybe 3 lines of the csv) and a clear description of what you'd like to be done to that data. Jun 30, 2021 at 14:43
• See 250336 for assistance in color-coding. Jun 30, 2021 at 15:11

Let's say, you have your data with three tags (first, second and third). I made it manually:

d = Flatten[
Table[{i, i^2 + RandomReal[30 i], j}, {i, 1,
50}, {j, {"f", "s", "t"}}]]


{1, 28.2759, "f", 1, 14.2023, "s", 1, 20.0824, "t", 2, 20.3635, "f",
2, 31.7858, "s", 2, 55.8098, "t",...}

Let's separate them by tags:

r = Table[#[[i ;; i + 2]], {i, 1, Length@# - 2, 3}] &@d;

c1 = Select[r, #[] == "f" &][[All, {1, 2}]];
c2 = Select[r, #[] == "s" &][[All, {1, 2}]];
c3 = Select[r, #[] == "t" &][[All, {1, 2}]];


It gives three lists of data that are plotable by ListPlot:

ListPlot[{c1, c2, c3}, PlotStyle -> {Red, Green, Blue},
Joined -> True, PlotLegends -> {"f", "s", "t"}] • Hi there, apologies for the delayed response. I'm having an issue with getting the data to be in a single list, my data is already separated e.g. {1, 28.2759, "f"}, {1, 14.2023, "s"}, {1, 20.0824, "t"}, {2, 20.3635, "f"},...} and it generates c1, c2 and c3 as empty lists i.e. {} Jul 12, 2021 at 14:33
• @Schaef, I guess, the problem is in patterns ("s","f", etc). Check the heads of third elements and correct the conditions for Select according to actual values. Jul 12, 2021 at 17:02

You can use GroupBy

For example using the toy data from @Rom38 answer:

d = Flatten[
Table[{i, i^2 + RandomReal[30 i], j}, {i, 1,
50}, {j, {"f", "s", "t"}}]];


You could do:

dt = Partition[d, 3];
ListPlot[GroupBy[dt, #[] &, #[[All, {1, 2}]] &], Joined -> True] 