# Counting points from csv file pgfplots

I am importing data from a .csv file to obtain a scatter plot, here's the MWE:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
jitter/.style={
x filter/.code={\pgfmathparse{\pgfmathresult+rnd*#1}},
y filter/.code={\pgfmathparse{\pgfmathresult+rnd*#1*0.1}}
},
jitter/.default=0.1
}

\begin{document}

%%% Victory Opening vs Victory Race
\begin{figure}
\begin{tikzpicture}
\begin{axis}[width=15cm,
axis background/.style={fill=blue!10},
xlabel={Victory Opening},
ylabel={Victory Race},
legend entries={Winners, Losers},
legend style={nodes=right},
legend pos= north east,
]
\addplot +[only marks,mark=o] table [col sep=comma,x=VictoryOpening,y=VictoryRace]{MapsFullNumerical.csv};
\addplot +[only marks,red,mark=x] table [col sep=comma,x=LossOpening,y=LossRace]{MapsFullNumerical.csv};
\end{axis}
\end{tikzpicture}
\caption{Victory Opening vs Victory Race}
\end{figure}
\end{document}


what I would like to do is count the number of points for each cluster. Here the first two lines of the .csv file:

NomeRep,VictoryOpening,VictoryRace,LossOpening,LossRace,numRegioni,numBasi,numStart,hopMedio,numChoke,numArchi,FlussoMedio,FlussoMedioBasi,FlussoMedioStarting,percPosStratReg,percPosStratBas,percPosStratStart,perimetroMedio,perimetroMedioBasi,perimetroMedioStarting,areaMedia,areaMediaBasi,areaMediaStarting,chokeMedio,chokeMedioBasi,chokeMedioStarting,perc chokePerimetro,perc chokePerimetroBasi,perc chokePerimetroStarting,mediaMinBasi,mediaMinStart,mediaGasBasi,mediaGasStart,blocchiMinBase,blocchiMinStarting,distanzaRegTerra,percraggRegTerra,distanzaRegTerra95,distanzaBasiTerra,percraggBasiTerra,distanzaBasiTerra95,distanzaStartTerra,percraggStartTerra,distanzaAriaBasi,distanzaAriaStartPoints,numRegNonRaggiungibili,gradoMedio,HR Start-Reg,HR Start-Base,HR Base-Reg,Min/Gas Ratio Basi,Min/Gas Ratio Start

1,5,3,8,1,21,16,8,3.3,23,22,315,323,299,28,30,50,3689,3300,3756,552668,442894,557594,1938,1259,924,52,43,24,4615,5000,12923,12000,8,8,2988,81,2855,3156,100,3024,4352,100,2263,2808,0,2,1.72,1.61,1.03,0.36,0.42


And here's the image obtained so far:

-
Could you provide some dummy data? The file is currently not compilable, because MapsFullNumerical.csv is missing. –  Jake Jun 9 '12 at 15:59
So in this application, the "number of points for each cluster" is the same for both series, and is equal to the number of lines in the file, right? –  Jake Jun 9 '12 at 16:09
Basically there are 3 races and 15 strategies, but each race uses just a subset of them, therefore the scatter plots show 15 clusters. What I would like to to is count the points belonging to each cluster. I'm including an image to be clearer. –  Edo Jun 9 '12 at 16:24
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 13 '14 at 11:33

Assuming all your .csv files have the same structure, you could do the following, using the xstring package. My \countpointsfromcsv counts the number of commas in the first line of your .csv file, adds one to that number, and prints the result. It prints "52" when applied to the csv data your provided.

\documentclass{article}

\usepackage{xstring}
\usepackage{filecontents}

\begin{filecontents*}{MapsFullNumerical.csv}
NomeRep,VictoryOpening,VictoryRace,LossOpening,LossRace,numRegioni,numBasi,numStart,hopMedio,numChoke,numArchi,FlussoMedio,FlussoMedioBasi,FlussoMedioStarting,percPosStratReg,percPosStratBas,percPosStratStart,perimetroMedio,perimetroMedioBasi,perimetroMedioStarting,areaMedia,areaMediaBasi,areaMediaStarting,chokeMedio,chokeMedioBasi,chokeMedioStarting,percchokePerimetro,perc chokePerimetroBasi,percchokePerimetroStarting,mediaMinBasi,mediaMinStart,mediaGasBasi,mediaGasStart,blocchiMinBase,blocchiMinStarting,distanzaRegTerra,percraggRegTerra,distanzaRegTerra95,distanzaBasiTerra,percraggBasiTerra,distanzaBasiTerra95,distanzaStartTerra,percraggStartTerra,distanzaAriaBasi,distanzaAriaStartPoints,numRegNonRaggiungibili,gradoMedio,HRStart-Reg,HR Start-Base,HR Base-Reg,Min/Gas Ratio Basi,Min/Gas Ratio Start

1,5,3,8,1,21,16,8,3.3,23,22,315,323,299,28,30,50,3689,3300,3756,552668,442894,557594,1938,1259,924,52,43,24,4615,5000,12923,12000,8,8,2988,81,2855,3156,100,3024,4352,100,2263,2808,0,2,1.72,1.61,1.03,0.36,0.42
\end{filecontents*}

\newcommand\countpointsfromcsv[1]{%
\StrCount{\mycsvlist}{,}[\numberminusone]%
\the\expandafter\numexpr\numberminusone+1\relax%
}

\begin{document}
\countpointsfromcsv{MapsFullNumerical.csv}
\end{document}


Reference (for \read): http://tex.stackexchange.com/a/101936/21891

-
Never put a \newread (except in special situations, this not being one of them) in the body of a definition: with your code every call of \countpointsfromcsv allocates a new input stream and there are only 16 of them. You should also close the stream after usage. –  egreg Apr 20 '13 at 23:05
@egreg Ok. Is the solution I link to one of those special situations? –  Jubobs Apr 20 '13 at 23:33
No. I didn't see that answer; it's a common mistake. –  egreg Apr 20 '13 at 23:36
@egreg Please don't hesitate to post a good answer to make things right. I'll delete mine, then. –  Jubobs Apr 20 '13 at 23:37
Why should I? Just fix it: move \newread\myread before \newcommand and add \closein\myread before the closing brace. –  egreg Apr 20 '13 at 23:41