1

I have a raw data file that is simulated as follows.

\begin{filecontents*}{raw.data}
0-2         3
2-4         4
4-6         8 
6-8         20  
8-10        20  
10-12       15
12-14       14
14-16       9
16-18       4
18-20       3
\end{filecontents*}

I want to plot the cumulative frequency "less than" or "less than or equal". My attempt is as follows but here I have to pre-process the raw data file into

\begin{filecontents*}{test.data}
0   0
2   3
4   7 
6   15  
8   35  
10  55
12  70
14  84
16  93
18  97
20  100 
\end{filecontents*}

It is very tedious job for me.

MWE

\documentclass[pstricks]{standalone}
\usepackage{pst-plot,filecontents}

\begin{filecontents*}{raw.data}
0-2         3
2-4         4
4-6         8 
6-8         20  
8-10        20  
10-12       15
12-14       14
14-16       9
16-18       4
18-20       3
\end{filecontents*}

\begin{filecontents*}{test.data}
0   0
2   3
4   7 
6   15  
8   35  
10  55
12  70
14  84
16  93
18  97
20  100 
\end{filecontents*}

\newpsstyle{mygrid}
{
    Dy=5,
    Dx=2,
    subticksize=1,
    xsubticks=4,
    ysubticks=5,
    subtickcolor=cyan!50,
    xAxisLabel=\huge Score,
    xAxisLabelPos={c,-5},
    yAxisLabel=\huge Cumulative Frequency,
    yAxisLabelPos={-1.5,c},
    llx=-2,
    lly=-2,
    urx=1,
    ury=1,  
}
\readdata{\mydata}{test.data}
\begin{document}
\begin{psgraph}[style=mygrid,xticksize=0 100,yticksize=0 20](0,0)(20,100){15cm}{20cm}
\listplot[plotstyle=cspline,linecolor=red,linewidth=2pt,showpoints]{\mydata}
\end{psgraph}
\end{document}

enter image description here

Question

Is there any better way to plot without manually pre-processing the raw data file?

4
  • It's not very hard to pre-process the data... awk -F "( +|-)" 'BEGIN {S=0; print 0, 0}{S += $3; print $2, S }' < raw.data
    – Thruston
    Commented Nov 20, 2018 at 19:22
  • @Thruston: Thank for your nice advice. However, I don't know about awk. Commented Nov 21, 2018 at 6:07
  • 1
    Lucky you! The joy of learning lies ahead! (only joking). blog.jpalardy.com/posts/why-learn-awk
    – Thruston
    Commented Nov 21, 2018 at 9:35
  • @Thruston: Unfortunately, no awk in Windows. Commented Nov 21, 2018 at 15:59

1 Answer 1

4

it is easier if you read the data file in an own macro and then modify the data. With the current PSTricks macros it is a bit tricky, because \readdata puts a D between all values. It is defined on PS level as /D {} def but can be used to manipulate data.

\documentclass[pstricks]{standalone}
\usepackage{pst-plot,filecontents}

\begin{filecontents*}{raw.data}
    0-2         3
    2-4         4
    4-6         8 
    6-8         20  
    8-10        20  
    10-12       15
    12-14       14
    14-16       9
    16-18       4
    18-20       3
\end{filecontents*}

\newpsstyle{mygrid}
{
    Dy=5,
    Dx=2,
    subticksize=1,
    xsubticks=4,
    ysubticks=5,
    subtickcolor=cyan!50,
    xAxisLabel=\huge Score,
    xAxisLabelPos={c,-5},
    yAxisLabel=\huge Cumulative Frequency,
    yAxisLabelPos={-1.5,c},
    llx=-2,
    lly=-2,
    urx=1,
    ury=1,  
}
\makeatletter
\newcount\yVal \yVal=0
\def\prepare#1{\gdef\tempData{ 0 0 }%
    \expandafter\prepare@i #1}% D x1-x2 D y D ...
\def\prepare@i #1D #2-#3 D #4 {%
    \yVal=\the\numexpr#4+\yVal\relax 
    \xdef\tempData{ \tempData #3 \the\yVal\space } 
    \@ifnextchar D{\prepare@i}{}%
}
\makeatother
\begin{document}

\readdata{\mydata}{raw.data}%
\prepare{\mydata}
\begin{psgraph}[style=mygrid,xticksize=0 100,yticksize=0 20](0,0)(20,100){15cm}{18cm}
    \psset{plotstyle=cspline,linecolor=red,linewidth=1.5pt,showpoints}
    \listplot{\tempData}
\end{psgraph}

\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .