2
$\begingroup$

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!

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – bRost03
    Jun 30 at 14:43
  • $\begingroup$ See 250336 for assistance in color-coding. $\endgroup$
    – bbgodfrey
    Jun 30 at 15:11
  • $\begingroup$ Do the answers here help you at all? $\endgroup$
    – Jason B.
    Jun 30 at 21:53
2
$\begingroup$

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, #[[3]] == "f" &][[All, {1, 2}]];
c2 = Select[r, #[[3]] == "s" &][[All, {1, 2}]];
c3 = Select[r, #[[3]] == "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"}]

enter image description here

$\endgroup$
2
  • $\begingroup$ 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. {} $\endgroup$
    – Schaef
    Jul 12 at 14:33
  • $\begingroup$ @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. $\endgroup$
    – Rom38
    Jul 12 at 17:02
1
$\begingroup$

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, #[[3]] &, #[[All, {1, 2}]] &], Joined -> True]

enter image description here

$\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.