0

I have a frequency curve as in the figure below:

frequency curve

I need dashed lines that can help to estimate number that scored between 42 to 76.

My questions are

  1. I need to draw two dashed vertical lines from 42 and 76 on the x axis. The two lines will meet the curve and will turn 90° left and hit the y axis at two respective points.

  2. The two respective points where the dashed lines meet the y axis will be displayed, if possible, using pin command or something similar.

Kindly help me with the Tikz-PGF codes for the above two questions. Here's where I am at, currently.

Minimum Working Example

\documentclass[tikz,border=3mm]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}

\begin {document} \begin {figure} \begin{tikzpicture} \begin{axis}[tick align=outside,tick pos=lower,width=15cm, xtick={10,20,...,100},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,ultra thick,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=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 } coordinatepos=1; \path (0,0) coordinate (O) (100,0) coordinate (br) (100,\pgfkeysvalueof{/pgfplots/ymax}) coordinate (tr); \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 {figure} \end {document}

Note: in the following thread, it was the other way round. The dashed lines originated from y axis, touched the curve and hit x axis. How to typeset a cumulative frequency curve with non-integer x axis ticks using Tikz and PGF, not pstricks

I tried to mimic the solution given in that thread but no success. Kindly help me, for I am still a newbie. I appreciate.

1 Answers1

2

The same strategy as under the previous thread works: add paths and compute intersections.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}


\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[tick align=outside,tick pos=lower,width=11cm,
xtick={10,20,...,100},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,ultra thick,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=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); 
\pgfplotsinvokeforeach{42,76}{%
\path[name path=v#1] (#1,0) coordinate (x#1)-- (x#1|-pmax);
\path[name intersections={of=v#1 and plot,by=i#1}];}
\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\%} };
\foreach \X in {42,76}
{\draw[dashed,semithick] (x\X) |- (br|-i\X) node[right]{$v_{\X}$};}
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

Please note that the figure was too wide for the article document class you have chosen, so I decreased the width. (The options tikz,border=3mm work for standalone but are not options of the article class.)

ADDENDUM:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}


\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[tick align=outside,tick pos=lower,width=11cm,
xtick={10,20,...,100},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,ultra thick,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=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) (9.5,0) coordinate (O')
(100,0) coordinate (br) (100,\pgfkeysvalueof{/pgfplots/ymax}) coordinate
(tr); 
\pgfplotsinvokeforeach{42,76}{%
\path[name path=v#1] (#1,0) coordinate (x#1)-- (x#1|-pmax);
\path[name intersections={of=v#1 and plot,by=i#1}];}
\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\%} };
\foreach \X in {42,76}
{\draw[dashed,semithick] (x\X) |- (O'|-i\X) node[left]{$v_{\X}$};}
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

  • Thank you for the response and clarification, I'm very grateful. Though, I need the dashed horizontal lines going towards v_42 and v_76 to face the other side (180° turn) and hit the opposite axis, ie, the y axis (cumulative frequency). Thank you. – Chika Odiliobi Jun 05 '20 at 18:04
  • 1
    @ChikaOdiliobi I added this. It is basically the same except that one has now take another horizontal coordinated and add the nodes on the left. –  Jun 05 '20 at 18:09
  • Beautiful. Thank you immensely. – Chika Odiliobi Jun 05 '20 at 18:42