从 csv 文件 pgfplots 中计算点数

从 csv 文件 pgfplots 中计算点数

我正在从 .csv 文件导入数据以获取散点图,这是 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}

我想要做的是计算每个簇的点数。这是 .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,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 

这是目前获得的图像: 在此处输入图片描述

答案1

xstring假设所有 .csv 文件都具有相同的结构,您可以使用该包执行以下操作。My\countpointsfromcsv计算 .csv 文件第一行的逗号数量,在该数字上加一,然后打印结果。当应用于您提供的 csv 数据时,它会打印“52”。

\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*}


\newread\myread%

\newcommand\countpointsfromcsv[1]{%
    \openin\myread=#1%
    \read\myread to \mycsvlist%
    \StrCount{\mycsvlist}{,}[\numberminusone]%
    \the\expandafter\numexpr\numberminusone+1\relax%
    \closein\myread%
}


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

参考(针对\read):https://tex.stackexchange.com/a/101936/21891

相关内容