TransWikia.com

Drawing vertical lines from given x-axis values to frequency curve intersections, then to the y axis

TeX - LaTeX Asked on February 2, 2021

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=pgfmathprintnumbertick,
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); 
end{axis}
draw[-stealth] (br) -- (tr);
path (br) -- (br|-pmax) foreach X in {0,20,...,100}
{coordinate[pos=X/100] (pX)
(pX) 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.

One Answer

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=pgfmathprintnumbertick,
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] (pX)
(pX) 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] (xX) |- (br|-iX) 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=pgfmathprintnumbertick,
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] (pX)
(pX) 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] (xX) |- (O'|-iX) node[left]{$v_{X}$};}
end{tikzpicture}
end{figure}
end{document}

enter image description here

Answered by user194703 on February 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP