# How to display the median on Ogive Curve using Tikz or pgfPlots

I am to plot an ogive curve and find the median of the given data by drawing a dotted line parallel to x-axis offset by 55 units and then drawing a line perpendicular to x-axis from the point of intersection.

I need to label the point of intersection to show its coordinates along with its abscissa & ordinate.

Just like in the figure shown below:

Here is the code I have so far:

\documentclass[12pt,a4paper]{article}

\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{pgfplots,tikz,siunitx,float,amsfonts,amsmath,amssymb}
\pgfplotsset{compat=1.16}% 1.16 would be better
\usetikzlibrary{intersections, positioning, calc, math}

\begin{document}
\noindent
{\bf Question:}\\
Construct less than cumulative frequency table \& draw the Ogive for the data given below.\\
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|} \hline
{\bf Marks}     & 1-10  & 10-20 & 20-30 & 30-40 & 40-50 & 50-60 & 60-70 & 70-80 & 80-90 & 90-100\\ \hline
{\bf Students}  & 3     & 4     & 12    & 18    & 20    & 25    & 12    & 8     & 6     & 2     \\ \hline
\end{tabular}

\vspace*{0.5cm}
\noindent
{\bf Solution:}
\begin{center}
\begin{tabular}{|c|c|c|} \hline
{\bf Data($<$UL)} & {\bf Freq(f)} & {\bf CF}\\ \hline
10  & 3     & 3     \\ \hline
20  & 4     & 7     \\ \hline
30  & 12    & 19    \\ \hline
40  & 18    & 37    \\ \hline
50  & 20    & 57    \\ \hline
60  & 25    & 82    \\ \hline
70  & 12    & 94    \\ \hline
80  & 8     & 102   \\ \hline
90  & 6     & 108   \\ \hline
100 & 2     & 110   \\ \hline
\end{tabular}

\vspace*{0.25cm}
{\bf Less-than type Cummulative Frequency Distribution Table}
\end{center}

As the last CF entry is 110. Hence,\vspace*{-0.5cm}
\begin{align*}
n &= 110 \\
\therefore \dfrac{n}{2} &= \dfrac{110}{2} \\
\dfrac{n}{2} &= 55 \\
\end{align*}
\vspace*{-1.5cm}
\begin{figure}[htb]
\centering
\begin{tikzpicture}

\begin{axis}[
axis lines=left, thick, grid=both,
width=0.8\textwidth, height=0.8\textwidth,
xmin=0,xmax=110, ymin=0,ymax=130, y=0.06cm,
xlabel={Upper Limits}, ylabel={Cumulative Frequency}
]

\addplot[color = black,smooth,very thick,mark=*,name path=h1] coordinates {
(10,3)(20,7)(30,19)(40,37)(50,57)(60,82)(70,94)(80,102)(90,108)(100,110)
};

\legend{Less-Than type Ogive Curve}
\end{axis}

\end{tikzpicture}
\caption{Less-Than type Ogive Curve.}
\end{figure}

\end{document}


How can I do this? I have tried using solutions mentioned at:
Tikz: display coordinate of node using let command
Intersections in Tikz and pgfplots and
Label intersection point of two curves in pgfplots

Irrelevant things first: \bf has been deprecated since the nineties I think, using \bfseries or \textbf{..} instead is recommended. Generally it would also probably be better to define some semantic markup as well, rather than all the manual formatting, line breaks etc., but I haven't done anything about that. To the question at hand.

One approach is to first make a \path at y = 55, and use the intersections library that you already load to find the intersection of that path and the plot. To get the values of the intersection point, you can use \pgfplotspointgetcoordinates (cf. https://tex.stackexchange.com/a/537916).

\documentclass[12pt,a4paper]{article}

\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{pgfplots,siunitx,float,amsfonts,amsmath,amssymb}
\pgfplotsset{compat=1.16}% 1.16 would be better
\usetikzlibrary{intersections, positioning, calc, math}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
axis lines=left, thick, grid=both,
width=0.8\textwidth, height=0.8\textwidth,
xmin=0,xmax=110, ymin=0,ymax=130, y=0.06cm,
xlabel={Upper Limits}, ylabel={Cumulative Frequency}
]

\addplot[color = black,smooth,very thick,mark=*,name path=h1] coordinates {
(10,3)(20,7)(30,19)(40,37)(50,57)(60,82)(70,94)(80,102)(90,108)(100,110)
};

\legend{Less-Than type Ogive Curve}

\newcommand\nhalf{55}

% first draw invisible path at y==55
\path [name path=y55] (\pgfkeysvalueof{/pgfplots/xmin},\nhalf) -- (\pgfkeysvalueof{/pgfplots/xmax}, \nhalf);
% then use the intersections library to calculate the intersection
% of that line and the plot
% draw dashed line via the calculated intersection
\draw [dashed, name intersections={of=h1 and y55, by={m}}] (0, \nhalf) -- (m) -- (m|- 0,0);

% after \pgfplotspointgetcoordinates{(m)} the x- and y-values are available
% with e.g. \pgfkeysvalueof{/data point/x}

\node [below right, red] at (m) {%
\pgfplotspointgetcoordinates{(m)}%
(\pgfmathprintnumber[precision=0]{\pgfkeysvalueof{/data point/x}},
\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}})
};

\draw [green, shorten <=3pt] (0,\nhalf) to[bend left] ++(1cm, 1cm) node[right] {$\frac{n}{2} = \nhalf$};
\draw [green, shorten <=3pt] (m|-0,0) -- ++(1cm, 1cm) node[right] {%
\pgfplotspointgetcoordinates{(m)}%
$\text{median} = \pgfmathprintnumber[precision=0]{\pgfkeysvalueof{/data point/x}}$};

\end{axis}

\end{tikzpicture}

\end{document}

• By the way, see tex.stackexchange.com/questions/401425 for an explanation of the (a -| b) syntax. May 18, 2021 at 14:21
• How can I label the coordinate of the intersection, Median=49 and the n/2=55 part? I know I can use \draw (0,55) node[above right]{$\dfrac{n}{2} = 55$} but is there a better way? as I don't know the coordinates of intersection point I won't be able to manually label the 'median'... May 18, 2021 at 14:28
• @YourPalNurav See update. May 18, 2021 at 15:55
• Thanks for your answer, helped me a lot! Could you please recommend a book or video/online resource from which I could learn more about tikz+pgfPlot? The official manual is quite extensive but is a lot complicated. Thanks again! May 19, 2021 at 6:41
• I think I've learned just about everything from this site and the two manuals ... There is also tex.stackexchange.com/q/15779, but there is nothing about pgfplots there I think. May 19, 2021 at 7:56