2
$\begingroup$

This must be simple, but I cannot seem to figure it or find the solution anywhere. I have data in 1000s of rows and 3 columns. Every thousands of rows have a unique tag id in the first column. I want to plot the 3rd column with the 2rd column as the x-axis with separate line for each tag id. I do the following, but how do I plot each line with a different color depending on the tagid and show the tagids at the end of each line on the top?

p = DeleteDuplicates[s\[Transpose][[1]]]
ListPlot[s[[All, {2, 3}]],{PlotLegends -> p}]

I suspect the problem is due to All in the ListPlot, but do not know a way around

Sample data can be found here.

Thanks for the help.

$\endgroup$

1 Answer 1

3
$\begingroup$
import = Import["SampleData.csv", "CSV"];

import = {1, 10^-9, 1} # & /@ import

The second line is to rescale the x values for clarity of the plot.

ids = DeleteDuplicates[import[[All, 1]]]

{15, 17, 39, 50, 53, 59, 75}

data = GatherBy[import, First];

s = data[[All, All, {2, 3}]];

ListLinePlot[s, Frame -> True, PlotLegends -> ids, FrameLabel -> {"x [10^9]", Rotate["y", 270 Degree]}]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ This is unnecessarily complex. Use GroupBy, instead, e.g. GroupBy[data, ToString[First[#]] & -> ({10^-9, 1} Rest[#] &)]. ListLinePlot will pick up the keys (now strings) automatically, and use them as legend labels. This reduces it to a one liner. $\endgroup$
    – rcollyer
    Sep 19, 2016 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.