1

I am a newbie here. I need your help:

I need to typeset this curve cumulative frequency curve using Tikz and pgf only, no pstricks. The xy values to be plotted are contained in the 3rd and 4th columns in this table: table

Note: I have seen the code in this related example, though I prefer non-integer x ticks: How to plot cumulative frequency from a raw data file? But this will not be useful for me because I want to avoid pstricks since it's clashing with some codes in my document.

Please I will be very grateful.

  • Since there is no closed form solution, you can enter the data from the table and fit a smooth curve, You can specify the tick values using [xtick={...}] or [xtick=data]. – John Kormylo Jun 04 '20 at 12:46
  • Thank you. What about the dashed line from 40%? Is there a code that will draw the horizontal dashed line from 40% to meet the smooth red curve and consequently, change course 90° downward and hit the x axis? – Chika Odiliobi Jun 04 '20 at 17:56

1 Answers1

4

One can let pgfplots accumulate the data, see e.g. https://tex.stackexchange.com/a/198397. For the dashed lines one can use intersections. For the percentage one can use the last point of the plot.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}
\newcounter{ihor}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=12cm,
    tick align=outside,tick pos=lower,
    xtick={9,19,...,99},xticklabel=\empty,
    minor tick style={draw=none},x tick style={draw=none},
    extra x ticks={9.5,19.5,...,99.5},
    extra x tick style={draw,grid style={draw=none},x tick style={draw},
        xticklabel=\pgfmathprintnumber\tick,
        xticklabel style={rotate=30}},
    axis x line=bottom,axis y line=left,    
    xmin=9,xmax=100,xlabel=Marks,
    ymin=0,ymax=55,minor tick num=4,ylabel=Cumulative frequency,
    grid=both,grid style={cyan},minor grid style={help lines,cyan},
    table/create on use/cumulative frequency/.style={% cf. https://tex.stackexchange.com/a/198397
        create col/expr={\pgfmathaccuma + \thisrow{frequency}}   
    }]
  \addplot[red,thick,name path global=plot,smooth,mark=+,mark options={color=black}] 
    table [x expr=9.5+10*\coordindex,
      y=cumulative frequency]{
  frequency
  2
  3
  4
  6
  13
  10
  5
  3
  2
  2
  } coordinate[pos=1](pmax);
  \path (0,0) coordinate (O)
    (100,0) coordinate (br) (100,\pgfkeysvalueof{/pgfplots/ymax}) coordinate
   (tr); 
  \setcounter{ihor}{0}
  \pgfplotsinvokeforeach{12.75,25.5,38.25}{%
  \stepcounter{ihor}
  \edef\temp{\noexpand\path[name path=hor-\number\value{ihor}]
   (\pgfkeysvalueof{/pgfplots/xmin},#1) -- (\pgfkeysvalueof{/pgfplots/xmax},#1);
  \noexpand\draw[dashed,name intersections={of=plot and hor-\number\value{ihor},
    by=i-\number\value{ihor}}] 
    (i-\number\value{ihor}|-O) \ifnum\value{ihor}=1
    node[above left]{$Q_{\arabic{ihor}}$} 
    \else
    node[above right]{$Q_{\arabic{ihor}}$} 
    \fi
        |-  (i-\number\value{ihor}-|O);}
   \temp        
  }
 \path (br) -- (pmax) coordinate[pos=0.4] (p40); 
 \path[name path=hor] (p40) -- (p40-|O);    
 \draw[dashed,name intersections={of=plot and hor,by=i}] 
    (br|-i) -| (br-|i) (i);
\end{axis}
\draw[-stealth] (br) -- (tr);
\path (br) -- (br|-pmax) foreach \X in {0,20,...,100}
 {coordinate[pos=\X/100] (p\X)
 (p\X) edge[help lines]++ (\pgfkeysvalueof{/pgfplots/minor tick length},0)
 node[pos=\X/100,right,xshift=\pgfkeysvalueof{/pgfplots/minor tick length}] {\X\%} };
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much. Your answer is very helpful. – Chika Odiliobi Jun 04 '20 at 17:09
  • It remains the dotted lines. Q_1, Q_2 and Q_3 mark out 12.75, 25.5 and 38.25 respectively on the left y axis. Is there a code that will enable one to draw the dashed line from say 12.75 on the y axis, the dashed line will meet the curve and automatically drop down and hit the x axis and label the point of contact Q_1, just as in the question? In other words, is there a better substitute to the following code for the dashed line Q_1? \draw [dashed] (8.5,12.75) -- (37.5,12.75) -- (37.5,0) node[] {$Q_1$}; – Chika Odiliobi Jun 04 '20 at 17:46
  • @ChikaOdiliobi One can use intersections. –  Jun 04 '20 at 18:02
  • Thank you. I really appreciate. You are such a gem. I still have 2 questions. Firstly, I tried scaling the whole graph by attaching [scale=1.5] at the back of \begin {tikzpicture}. Everything scaled, except the percentage axis. How can I overcome this? Secondly, the fourth line emanating from 40% on the percentage axis. Kindly help me with the code too. Thanks. – Chika Odiliobi Jun 04 '20 at 20:12
  • 1
    @ChikaOdiliobi One should never change the size of a pgfplots axis by scaling the ambient tikzpicture. Rather, use the width and height keys of the axis. I added the dashed line. I also would kindly ask you to ask additional requests in form of new questions. –  Jun 04 '20 at 20:32
  • Thank you so much. – Chika Odiliobi Jun 04 '20 at 20:59