## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Approximation Lines on Graph Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

### Approximation Lines on Graph

Hey guys,

I'm struggling with finding out how to draw intercept lines on a graph to find approximate values on a graph like the red and blue lines below.

Screenshot 2020-03-01 at 06.11.23.png (156.59 KiB) Viewed 458 times

I'm trying to add lines from the y-axis at 100 down to approximately 11 on the x-axis.

\documentclass[border=5pt]{standalone}\usepackage{pgfplots}\pgfplotsset{compat=1.16} \begin{document}\begin{tikzpicture}\begin{axis}[xlabel={Metres [m]},xtick align=outside,xmin=0,xmax=20,ylabel={Cost [£]},ytick align=outside,ymin=0,ymax=180,axis lines=left,ymajorgrids=true,xmajorgrids=true,grid style=dashed,]\addplotcoordinates {(1,9) (2,18) (4,36) (5,45) (10,90) (20,180)};\end{axis}\end{tikzpicture}\end{document}

Bartman
Posts: 77
Joined: Fri Jan 03, 2020 2:39 pm
pgf/TikZ offers the intersections library to solve this task:

\documentclass[border=5pt]{standalone}\usepackage{pgfplots}% loads the package tikz\pgfplotsset{compat=1.16} \usetikzlibrary{intersections} \begin{document}\begin{tikzpicture}\begin{axis}[    xlabel={Metres [m]},%    xtick align=outside,    xmin=0,    xmax=20,    ylabel={Cost [£]},%    ytick align=outside,    ymin=0,    ymax=180,    axis lines=left,    grid=major,% <- changed    grid style=dashed,]\addplot+ [name path=graph, black, mark options={black}]    coordinates {(1,9) (2,18) (4,36) (5,45) (10,90) (20,180)};\path [name path=y100]     (0,100) -- (0,100-|current axis.east);\draw [    -Stealth,    red,     dashed,    name intersections={of=y100 and graph, by=is}]     (is|-current axis.origin) edge (is)    (is) edge (0,100);\end{axis}\end{tikzpicture}\end{document}

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm
To the rescue again! Much appreciated.

Even now knowing that this is called intersections, I'm still none the wiser having looked it up in the manual! It's just what I'm after apart from the arrows should be on the other side of the dashed lines. Try as I may I can't even figure out to do this! If you could point (pun intended) me in the right direction that, again, would be greatly appreciated.

Could you recommend any decent learning sources for bettering my Tex knowledge? I'm working my way through several books. I guess most of its learnt by doing, but it would be good to know if there is any online courses that I may have missed.

Bartman
Posts: 77
Joined: Fri Jan 03, 2020 2:39 pm
tpa77l3 wrote:It's just what I'm after apart from the arrows should be on the other side of the dashed lines. Try as I may I can't even figure out to do this!

I accidentally used the command \draw and should have used \path instead. In this case it may work, but unfortunately not for your purpose. You have to replace \draw with \path and swap the node names so that you get the desired position of the arrows.

tpa77l3 wrote:Could you recommend any decent learning sources for bettering my Tex knowledge? I'm working my way through several books. I guess most of its learnt by doing, but it would be good to know if there is any online courses that I may have missed.

I am not familiar with LaTeX introductions in English. In my experience, the package documentation and the posts in the forums about pgfplots and pgf/TikZ are sufficient to familiarize yourself with the matter. Of course you need a lot of time to practice.

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm
Thanks for the reply. I'm still having trouble figuring out what needs swapping. Sorry for being dense!

Bartman
Posts: 77
Joined: Fri Jan 03, 2020 2:39 pm
The documentation describes the content of the parentheses as coordinates. A lot can be inserted at this point: the x and y value of a coordinate, the name of a node, the name of a coordinate determined with the \coordinate command or the name of a calculated intersection point, etc. Combinations of these, as shown in the example, are also permitted. In your case, the round pairs of parentheses must be swapped, which are before and after the word edge.

A simplified example of my code:

\draw [...]% replace the command for your purpose, but keep the options    (a) edge (b)% swap    (b) edge (c)% the same as before;