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


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


%%% Victory Opening vs Victory Race
                    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};
    \caption{Victory Opening vs Victory Race}

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


And here's the image obtained so far: enter image description here

  • 2
    Could you provide some dummy data? The file is currently not compilable, because MapsFullNumerical.csv is missing. – Jake Jun 9 '12 at 15:59
  • My bad, hadn't thought about that. – gcedo Jun 9 '12 at 16:02
  • 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. – gcedo 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. – jub0bs 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.



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



    \read\myread to \mycsvlist%


Reference (for \read): https://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? – jub0bs 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. – jub0bs Apr 20 '13 at 23:37
  • 2
    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

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.