LaTeX forum ⇒ Graphics, Figures & TablesApproximation 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

Postby tpa77l3 » Sun Mar 01, 2020 8:29 am

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
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.

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass[border=5pt]{standalone}
  2. \usepackage{pgfplots}
  3. \pgfplotsset{compat=1.16}
  4.  
  5. \begin{document}
  6. \begin{tikzpicture}
  7. \begin{axis}[
  8. xlabel={Metres [m]},
  9. xtick align=outside,
  10. xmin=0,
  11. xmax=20,
  12. ylabel={Cost [£]},
  13. ytick align=outside,
  14. ymin=0,
  15. ymax=180,
  16. axis lines=left,
  17. ymajorgrids=true,
  18. xmajorgrids=true,
  19. grid style=dashed,
  20. ]
  21. \addplot
  22. coordinates {(1,9) (2,18) (4,36) (5,45) (10,90) (20,180)}
  23. ;
  24. \end{axis}
  25. \end{tikzpicture}
  26. \end{document}


Thanks in advance!

Bartman
Posts: 77
Joined: Fri Jan 03, 2020 2:39 pm

Postby Bartman » Sun Mar 01, 2020 3:13 pm

pgf/TikZ offers the intersections library to solve this task:

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass[border=5pt]{standalone}
  2. \usepackage{pgfplots}% loads the package tikz
  3. \pgfplotsset{compat=1.16}
  4.  
  5. \usetikzlibrary{intersections}
  6.  
  7. \begin{document}
  8. \begin{tikzpicture}
  9. \begin{axis}[
  10. xlabel={Metres [m]},
  11. % xtick align=outside,
  12. xmin=0,
  13. xmax=20,
  14. ylabel={Cost [£]},
  15. % ytick align=outside,
  16. ymin=0,
  17. ymax=180,
  18. axis lines=left,
  19. grid=major,% <- changed
  20. grid style=dashed,
  21. ]
  22. \addplot+ [name path=graph, black, mark options={black}]
  23. coordinates {(1,9) (2,18) (4,36) (5,45) (10,90) (20,180)}
  24. ;
  25. \path [name path=y100]
  26. (0,100) -- (0,100-|current axis.east)
  27. ;
  28. \draw [
  29. -Stealth,
  30. red,
  31. dashed,
  32. name intersections={of=y100 and graph, by=is}
  33. ]
  34. (is|-current axis.origin) edge (is)
  35. (is) edge (0,100)
  36. ;
  37. \end{axis}
  38. \end{tikzpicture}
  39. \end{document}

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Postby tpa77l3 » Sun Mar 01, 2020 9:30 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

Postby Bartman » Mon Mar 02, 2020 12:56 am

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

Postby tpa77l3 » Mon Mar 02, 2020 7:51 am

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

Postby Bartman » Mon Mar 02, 2020 3:58 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:

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \draw [...]% replace the command for your purpose, but keep the options
  2. (a) edge (b)% swap
  3. (b) edge (c)% the same as before
  4. ;

tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Postby tpa77l3 » Tue Mar 03, 2020 7:42 am

Got it- many thanks.


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 4 guests