{"id":316,"date":"2022-02-14T02:34:34","date_gmt":"2022-02-13T18:34:34","guid":{"rendered":"https:\/\/www.scijournal.org\/articles\/?p=316"},"modified":"2023-05-22T06:13:04","modified_gmt":"2023-05-21T22:13:04","slug":"graphs-in-latex","status":"publish","type":"post","link":"https:\/\/www.scijournal.org\/articles\/graphs-in-latex","title":{"rendered":"How to Draw Graphs in LaTeX &#8211; Vector Graphics With TikZ"},"content":{"rendered":"\n<p>LaTeX is a versatile document creation system that can handle many complex tasks. Earlier we discussed different ways to create plots in LaTeX. You may be surprised to learn that it is also possible to create highly detailed <em>graphs in LaTeX<\/em> and customize them to your liking.<\/p>\n\n\n\n<p>This guide will show you how to make graphs in Latex using the TikZ package and tailor them according to your document requirements.&nbsp;<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_67_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69b406fff2658\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69b406fff2658\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#How_Does_LaTeX_Create_Graphs\" title=\"How Does LaTeX Create Graphs?\">How Does LaTeX Create Graphs?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Understanding_How_Graphs_in_latex_are_Created\" title=\"Understanding How Graphs in latex are Created\">Understanding How Graphs in latex are Created<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Creating_Bullet_Graphs_in_LaTeX\" title=\"Creating Bullet Graphs in LaTeX\">Creating Bullet Graphs in LaTeX<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Creating_a_Graph_With_a_Reversed_Y-Axis\" title=\"Creating a Graph With a Reversed Y-Axis\">Creating a Graph With a Reversed Y-Axis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Creating_Graphs_With_Different_Axis_Points\" title=\"Creating Graphs With Different Axis Points\">Creating Graphs With Different Axis Points<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Enclosing_Graph_Points_in_a_Frame\" title=\"Enclosing Graph Points in a Frame\">Enclosing Graph Points in a Frame<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Creating_Directed_Graphs_in_LaTeX\" title=\"Creating Directed Graphs in LaTeX\">Creating Directed Graphs in LaTeX<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Creating_Directed_Graphs_in_Latex_With_Arrows\" title=\"Creating Directed Graphs in Latex With Arrows\">Creating Directed Graphs in Latex With Arrows<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Drawing_Bent_Edges_Between_Vertices\" title=\"Drawing Bent Edges Between Vertices\">Drawing Bent Edges Between Vertices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Adding_Labels_to_Edges_in_Graphs\" title=\"Adding Labels to Edges in Graphs\">Adding Labels to Edges in Graphs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Adding_Colored_Vertices_to_Graphs\" title=\"Adding Colored Vertices to Graphs\">Adding Colored Vertices to Graphs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Adding_Colored_Edges_to_Graphs\" title=\"Adding Colored Edges to Graphs\">Adding Colored Edges to Graphs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Creating_Bar_Graphs_in_LaTeX\" title=\"Creating Bar Graphs in LaTeX\">Creating Bar Graphs in LaTeX<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Final_Thoughts\" title=\"Final Thoughts\">Final Thoughts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Frequently_Asked_Questions\" title=\"Frequently Asked Questions\">Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Q1_Can_LaTeX_draw_graphs\" title=\"Q1. Can LaTeX draw graphs?\">Q1. Can LaTeX draw graphs?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Q2_How_do_I_make_a_simple_graph_in_LaTeX\" title=\"Q2. How do I make a simple graph in LaTeX?\">Q2. How do I make a simple graph in LaTeX?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Q3_How_do_you_plot_a_line_in_LaTeX\" title=\"Q3. How do you plot a line in LaTeX?\">Q3. How do you plot a line in LaTeX?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\/#Further_Reading\" title=\"Further Reading\">Further Reading<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"how-does-latex-create-graphs\"><span class=\"ez-toc-section\" id=\"How_Does_LaTeX_Create_Graphs\"><\/span>How Does LaTeX Create Graphs?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You might be wondering how to draw graphs in LaTeX is capable of creating 2D and 3D graphs. After all, this is something that most other high-end document processing software such as MS-Word cannot do.&nbsp;<\/p>\n\n\n\n<p>The truth is that LaTeX cannot create complex graphs on its own. The system was originally designed at a time when computers possessed little processing capabilities, and this made it next to impossible to graphs in Latex for the simple act of inserting it into a document.<\/p>\n\n\n\n<p>In the present day, LaTeX creates complex graphs with the help of the <em>pgfplots <\/em>package. This is a special package based on the <em>TikZ<\/em> tool, that is widely used to create scientific and technical graphics.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"understanding-how-graphs-in-latex-are-created\"><span class=\"ez-toc-section\" id=\"Understanding_How_Graphs_in_latex_are_Created\"><\/span>Understanding How Graphs in latex are Created<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we examine how to create graphs in LaTeX, it is worth understanding what exactly a graph is and how they are typically plotted. Graphs are generally composed of a set of vertices and a set of edges. These edges typically connect two vertices, and they may be directed or undirected.<\/p>\n\n\n\n<p>For example, let\u2019s say we wanted to create graphs in latex consisting of points connected by a line. Our first step is to plot each point in our space and then direct an edge to connect various points in a specified direction. However, it is possible to create graphs in latex consisting only of vertices and no edges or lines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-bullet-graphs-in-latex\"><span class=\"ez-toc-section\" id=\"Creating_Bullet_Graphs_in_LaTeX\"><\/span>Creating Bullet Graphs in LaTeX<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A bullet graph is a graph consisting of points plotted onto a space defined by axes. The points in these graphs are not connected by a line. Creating bullet graphs in LaTeX is simple, and can be accomplished using a few key pieces of code. These demonstrate this in the following example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n\\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::} \n!{(0,0) }*+ {\\bullet_{a}}\n!{(0,3) }*+ {\\bullet_{b}} \n!{(3,1) }*+ {\\bullet_{c}}  \n!{(5,-1)}*+ {\\bullet_{d}}  \n}  \n\\end{document} \n\n<\/pre><\/div>\n\n\n<p>Compiling the above code should produce the graphs in latex with four points labeled a, b, c, and d. We started by first using the command \\usepackage[all]{xy} in the document preamble. This command tells LaTeX to define all packages at once and to use XY axes for drawing the graph.<\/p>\n\n\n\n<p>In the code <em>!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}<\/em>, <em>&lt;0cm,0cm&gt;<\/em> sets the graph\u2019s origin to the coordinate <em>(0,0)<\/em>. The next two parameters <em>&lt;1cm,0cm&gt;<\/em> and <em>&lt;0cm,1cm&gt;<\/em> specify the lengths of the graph\u2019s <em>x<\/em> and&nbsp; <em>y<\/em> axes.<\/p>\n\n\n\n<p>We then added points or \u201cvertices\u201dto our graph using the command <em>!{(&#8230;,&#8230;) + {\\bullet_{YOURLABEL}<\/em>. In this code, the values inside <em>(&#8230;,&#8230;)<\/em> specify the <em>x<\/em> and&nbsp; <em>y<\/em> coordinates for the node, and <em>YOURLABEL <\/em>indicates the label you wish to give your node.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-a-graph-with-a-reversed-y-axis\"><span class=\"ez-toc-section\" id=\"Creating_a_Graph_With_a_Reversed_Y-Axis\"><\/span>Creating a Graph With a Reversed Y-Axis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now let\u2019s try the same example again, but with a reversed y-axis:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n\\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,-1cm&gt;::} \n!{(0,0) }*+ {\\bullet_{a}}\n!{(0,3) }*+ {\\bullet_{b}} \n!{(3,1) }*+ {\\bullet_{c}}  \n!{(5,-1)}*+ {\\bullet_{d}}  \n}  \n\\end{document}\n\n<\/pre><\/div>\n\n\n<p>When compiled, the graph looks identical to the one we created earlier, except that it is mirrored about the x-axis. Note that we simply changed the y-axis parameter in the code <em>!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}<\/em> from the original graph to <em>!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,-1cm&gt;::}<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-graphs-with-different-axis-points\"><span class=\"ez-toc-section\" id=\"Creating_Graphs_With_Different_Axis_Points\"><\/span>Creating Graphs With Different Axis Points<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is also possible to create a bullet graph with smaller <em>xy<\/em> axes scales. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n\\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;0.5cm,0cm&gt;:&lt;0cm,0.5cm&gt;::} \n!{(0,0) }*+ {\\bullet_{a}}\n!{(0,3) }*+ {\\bullet_{b}} \n!{(3,1) }*+ {\\bullet_{c}}  \n!{(5,-1)}*+ {\\bullet_{d}}  \n}  \n\\end{document} \n<\/pre><\/div>\n\n\n<p>Our new graph looks similar to the first one we created. However, the points are now half the distance to the origin than they previously were. We achieved this by changing the code from <em>!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::} <\/em>to <em>!{&lt;0cm,0cm&gt;;&lt;0.5cm,0cm&gt;:&lt;0cm,0.5cm&gt;::} <\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enclosing-graph-points-in-a-frame\"><span class=\"ez-toc-section\" id=\"Enclosing_Graph_Points_in_a_Frame\"><\/span>Enclosing Graph Points in a Frame<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In some cases, you may wish to enclose your graph inside a frame. This could be to present multiple graphs side-by-side neatly or to simply add an extra border to your graph. You can add a box around your graph using the command <em>\\[ \\fbox &#8230;YOURGRAPHCODE&#8230;..\\]<\/em><\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n\\&#x5B; \\fbox{\\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::} \n!{(0,0) }*+ {\\bullet_{a}}\n!{(0,3) }*+ {\\bullet_{b}} \n!{(3,1) }*+ {\\bullet_{c}}  \n!{(5,-1)}*+ {\\bullet_{d}}  \n}} \\] \n\\end{document} \n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-directed-graphs-in-latex\"><span class=\"ez-toc-section\" id=\"Creating_Directed_Graphs_in_LaTeX\"><\/span>Creating Directed Graphs in LaTeX<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As mentioned earlier, it is possible to create directed graphs in LaTeX. A directed graphs in latex consists of vertices that are connected via directed edges. These edges appear as lines between vertices. We demonstrate how to create a simple directed graph using the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n \\&#x5B; \\fbox{ \\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}  \n!{(0,0) }*+{\\bullet_{a}}=&quot;a&quot;  \n!{(0,3) }*+{\\bullet_{b}}=&quot;b&quot;  \n!{(3,1) }*+{\\bullet_{c}}=&quot;c&quot;  \n!{(5,-1)}*+{\\bullet_{d}}=&quot;d&quot;  \n&quot;a&quot;-&quot;b&quot; &quot;d&quot;-&quot;a&quot;  \n&quot;b&quot;-&quot;c&quot;  \n&quot;c&quot;-&quot;d&quot;  \n&quot;d&quot;-&quot;c&quot;  \n} } \\]  \n  \n\\end{document} \n<\/pre><\/div>\n\n\n<p>In the above example, the code <em>&#8220;a&#8221;-&#8220;b&#8221; &#8220;d&#8221;-&#8220;a&#8221;&nbsp; &#8220;b&#8221;-&#8220;c&#8221;&nbsp; &#8220;c&#8221;-&#8220;d&#8221;&nbsp; &#8220;d&#8221;-&#8220;c&#8221;<\/em> describes the path connecting various points. So the path that connects point <em>a<\/em> with point <em>b<\/em> is represented as <em>&#8220;a&#8221;-&#8220;b&#8221;<\/em> in the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-directed-graphs-in-latex-with-arrows\"><span class=\"ez-toc-section\" id=\"Creating_Directed_Graphs_in_Latex_With_Arrows\"><\/span>Creating Directed Graphs in Latex With Arrows<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The previous example showed line paths between points, but without any direction. It is possible to add direction to your line paths and represent them as arrows. We demonstrate how to do this in the following example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n \\&#x5B; \\fbox{ \\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}  \n!{(0,0) }*+{\\bullet_{a}}=&quot;a&quot;  \n!{(0,3) }*+{\\bullet_{b}}=&quot;b&quot;  \n!{(3,1) }*+{\\bullet_{c}}=&quot;c&quot;  \n!{(5,-1)}*+{\\bullet_{d}}=&quot;d&quot;  \n&quot;a&quot;:&quot;b&quot; &quot;d&quot;:&quot;a&quot;  \n&quot;b&quot;:&quot;c&quot;  \n&quot;c&quot;:&quot;d&quot;  \n&quot;d&quot;:&quot;c&quot;  \n} } \\]  \n  \n\\end{document} \n<\/pre><\/div>\n\n\n<p>The new graph shows arrows directing from <em>a<\/em> to <em>b<\/em>, <em>b<\/em> to <em>c<\/em>, <em>d<\/em> to <em>c<\/em>, <em>a<\/em> to <em>d<\/em>, and <em>d<\/em> to <em>a<\/em>. Notice that we simply swapped out the \u201c<em>&#8211;<\/em>\u201d in the earlier code with \u201c<em>:<\/em>\u201d to achieve this. When used properly, you can create an intricate direction path between vertices for your graphs in LaTeX<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"drawing-bent-edges-between-vertices\"><span class=\"ez-toc-section\" id=\"Drawing_Bent_Edges_Between_Vertices\"><\/span>Drawing Bent Edges Between Vertices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you wish to create the graphs in latex with vertices connected via bending edges, you must use \u201c<em>-@\/^\/<\/em>\u201d between the vertices you are specifying. If we apply this to the above example, the code becomes:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n \\&#x5B; \\fbox{ \\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}  \n!{(0,0) }*+{\\bullet_{a}}=&quot;a&quot;  \n!{(0,3) }*+{\\bullet_{b}}=&quot;b&quot;  \n!{(3,1) }*+{\\bullet_{c}}=&quot;c&quot;  \n!{(5,-1)}*+{\\bullet_{d}}=&quot;d&quot;  \n&quot;a&quot;:&quot;b&quot; &quot;d&quot;:&quot;a&quot;  \n&quot;b&quot;:&quot;c&quot;  \n&quot;c&quot;-@\/^\/&quot;d&quot;  \n&quot;d&quot;-@\/^\/&quot;c&quot;  \n} } \\]  \n\n<\/pre><\/div>\n\n\n<p>The new graph shows two bent lines connecting vertices <em>c<\/em> and <em>d<\/em>, while the other lines are straight directional ones with arrows.<\/p>\n\n\n\n<p>It is also possible to set the length of the bending edge using the command <em>-@\/^&#8230;cm\/ <\/em>.&nbsp; We demonstrate this in the following example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n  \n\\begin{document}  \n  \n \\&#x5B; \\fbox{ \\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}  \n!{(0,0) }*+{\\bullet_{a}}=&quot;a&quot;  \n!{(2,3) }*+{\\bullet_{b}}=&quot;b&quot;  \n!{(3,1) }*+{\\bullet_{c}}=&quot;c&quot;  \n!{(2,-1)}*+{\\bullet_{d}}=&quot;d&quot;  \n&quot;a&quot;-&quot;b&quot;  \n&quot;b&quot;-@\/^3cm\/&quot;d&quot;  \n&quot;d&quot;-&quot;c&quot;  \n&quot;a&quot;-@\/^\/&quot;d&quot;  \n&quot;b&quot;-@\/^2cm\/&quot;c&quot; \n} } \\]  \n<\/pre><\/div>\n\n\n<p>The above command sets the length of the bent edge connecting <em>b <\/em>and <em>d<\/em> to 3cm, and the one connecting <em>b <\/em>and <em>c<\/em> to 2cm. Having the option of customizing the bent edge length is useful, as it allows you to make a complex collection of edges appear neater and less cluttered.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-labels-to-edges-in-graphs\"><span class=\"ez-toc-section\" id=\"Adding_Labels_to_Edges_in_Graphs\"><\/span>Adding Labels to Edges in Graphs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In some cases, you may wish to add a label to the edges of your graph. These labels appear similar to the ones around vertices, except that they are above their corresponding edge. In the code, the label appears after the edge command. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n&quot;a&quot;-&quot;b&quot;^(0.5)(h_1)  \n&quot;b&quot;-@\/^3cm\/&quot;d&quot;(0.5)(i_2)\n&quot;d&quot;-&quot;c&quot;^(0.4)(j_3)  \n&quot;a&quot;-@\/^\/&quot;d&quot;^(0.6)(k_5)  \n&quot;b&quot;-@\/^2cm\/&quot;c&quot;^(0.4)(i_4)\n} } \\]  \n<\/pre><\/div>\n\n\n<p>In the above code, <em>&#8220;a&#8221;-&#8220;b&#8221;^(0.5)(h_1) <\/em>sets the label <em>h_1<\/em> a distance of 0.5 from the <em>a-b<\/em> edge.&nbsp;<\/p>\n\n\n\n<p>Similarly, <em>&#8220;b&#8221;-@\/^3cm\/&#8221;d&#8221;(0.5)(i_2)<\/em> sets the label <em>i_2<\/em> a distance of 0.5 from the <em>b-d<\/em> edge.<\/p>\n\n\n\n<p><em>&#8220;d&#8221;-&#8220;c&#8221;^(0.4)(j_3) <\/em>sets the label <em>j_3<\/em> a distance of 0.4 from the <em>d-c<\/em> edge.<\/p>\n\n\n\n<p>&#8220;a&#8221;-@\/^\/&#8221;d&#8221;^(0.6)(k_5) sets the label <em>k_5<\/em> a distance of 0.6 from the <em>a-d<\/em> edge.<\/p>\n\n\n\n<p><em>&#8220;b&#8221;-@\/^2cm\/&#8221;c&#8221;^(0.4)(i_4)<\/em> sets the label <em>i_4<\/em> a distance of 0.4 from the <em>b-c<\/em> edge.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-colored-vertices-to-graphs\"><span class=\"ez-toc-section\" id=\"Adding_Colored_Vertices_to_Graphs\"><\/span>Adding Colored Vertices to Graphs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>LaTeX also allows you to create graphs with colored vertices. You can do this by adding the color name before the command <em>\\bullet<\/em>. However, you will also need to use the package <em>color<\/em> to add colors to your graph. An example of this code is shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n\\usepackage{xcolor}  \n\\begin{document}  \n  \n \\&#x5B; \\fbox{ \\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}  \n!{(0,0) }*+&#x5B;green]{\\bullet_{a}}=&quot;a&quot;  \n!{(0,3) }*+{\\bullet_{b}}=&quot;b&quot;  \n!{(3,1) }*+&#x5B;blue]{\\bullet_{c}}=&quot;c&quot;  \n!{(5,-1)}*+&#x5B;red]{\\bullet_{d}}=&quot;d&quot;  \n&quot;a&quot;:&quot;b&quot; &quot;d&quot;:&quot;a&quot;  \n&quot;b&quot;:&quot;c&quot;  \n&quot;c&quot;-@\/^\/&quot;d&quot;  \n&quot;d&quot;-@\/^\/&quot;c&quot;  \n} } \\]  \n<\/pre><\/div>\n\n\n<p>The above code assigns a red vertice for <em>a<\/em>, a blue vertice for <em>b<\/em>, and a <em>red<\/em> vertice for <em>d<\/em>. The vertice for <em>c<\/em> remains black as we did not specify a color for it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-colored-edges-to-graphs\"><span class=\"ez-toc-section\" id=\"Adding_Colored_Edges_to_Graphs\"><\/span>Adding Colored Edges to Graphs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The process of adding colored edges to graphs is similar to the above. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\documentclass&#x5B;12pt]{article}  \n\\usepackage&#x5B;all]{xy}  \n\\usepackage{xcolor}  \n\\begin{document}  \n  \n \\&#x5B; \\fbox{ \\xygraph{  \n!{&lt;0cm,0cm&gt;;&lt;1cm,0cm&gt;:&lt;0cm,1cm&gt;::}  \n!{(0,0) }*+{\\bullet_{a}}=&quot;a&quot;  \n!{(0,3) }*+{\\bullet_{b}}=&quot;b&quot;  \n!{(3,1) }*+{\\bullet_{c}}=&quot;c&quot;  \n!{(5,-1)}*+{\\bullet_{d}}=&quot;d&quot;  \n&quot;a&quot;:&quot;b&quot; &quot;d&quot;:&#x5B;green]&quot;a&quot;  \n&quot;b&quot;:&quot;c&quot;  \n&quot;b&quot;-@\/^\/&#x5B;orange]&quot;d&quot;  \n&quot;d&quot;-@\/^\/&#x5B;red]&quot;c&quot;  \n} } \\]  \n<\/pre><\/div>\n\n\n<p>The above code adds a green edge between <em>d<\/em> and <em>a,<\/em> an orange edge between <em>d<\/em> and <em>c<\/em>, and a red edge between <em>b<\/em> and <em>d<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-bar-graphs-in-latex\"><span class=\"ez-toc-section\" id=\"Creating_Bar_Graphs_in_LaTeX\"><\/span>Creating Bar Graphs in LaTeX<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>LaTeX allows you to create detailed bar graphs in latex thanks to <em>pgfplots<\/em>. These bar graphs are highly customizable, so you can tailor them to your liking in many ways. Let\u2019s start with the example code below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\begin{tikzpicture}\n\\begin{axis}&#x5B;\n\tx tick label style={\n\t\t\/pgf\/number format\/1000 sep=},\n\tylabel=Pounds of Fish Caught,\n\tenlargelimits=0.06,\n\tlegend style={at={(0.5,-0.1)},\n\tanchor=north,legend columns=-1},\n\tybar interval=0.7,\n]\n\\addplot \n\tcoordinates {(2010,2313) (2009,3872)\n\t\t (2008,1758) (2007,3257)};\n\\addplot \n\tcoordinates {(2010,1783) (2009,2306) \n\t\t(2008,1854) (207,3717)};\n\\legend{Ship1,Ship2}\n\\end{axis}\n\\end{tikzpicture}\n\n<\/pre><\/div>\n\n\n<p>In the above code, we began our code with the command <em>\\begin{tikzpicture}<\/em> to create a <em>tikzpicture<\/em> environment. This environment is necessary for <em>pgfplots<\/em> to load.<\/p>\n\n\n\n<p>Next we defined the plot style using the code:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\begin{axis}&#x5B;\n\tx tick label style={\n\t\t\/pgf\/number format\/1000 sep=}\n\n<\/pre><\/div>\n\n\n<p>The code <em>enlargelimits=0.06<\/em> tells LaTeX to enlarge the limits within the bar plot. This is often needed because bar charts need extra room above each bar to look neater. This is especially useful if you wish to add labels on top of the bars. The parameter <em>0.06<\/em> is relative to the bar plot\u2019s total height.<\/p>\n\n\n\n<p>The code <em>legend style={at={(0.5,-0.1)}, anchor=north,legend columns=-1}<\/em> sets the legend\u2019s style. Changing the second parameter <em>-0.1<\/em> in this code would move the legend closer or farther from the x-axis.<\/p>\n\n\n\n<p>The code <em>ybar interval=0.7<\/em> sets the thickness of the bars. Setting this to <em>1<\/em> sets the bars directly next to each other and with no gap. Setting this to <em>0<\/em> makes the bars so narrow they appear as lines.<\/p>\n\n\n\n<p>We then added bar plots for each series. For <em>Ship1<\/em> we used the code&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: latex; title: ; notranslate\" title=\"\">\n\\addplot \n\tcoordinates {(2010,2313) (2009,3872)\n\t\t (2008,1758) (2007,3257)};\n\n<\/pre><\/div>\n\n\n<p>The above coordinates sets a bar chart with a height of 2313 in 2010, 3872 in 2009, and 1758 in 2008.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"final-thoughts\"><span class=\"ez-toc-section\" id=\"Final_Thoughts\"><\/span>Final Thoughts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As you can see, LaTeX offers many ways to create graphs for your documents. Creating graphs in LaTeX becomes a relatively straightforward process once you understand the specific commands involved.&nbsp;<\/p>\n\n\n\n<p>You should familiarize yourself with the codes used above if you wish to create customized graphs in latex that are tailored to your requirements. With a bit of practice, you should be able to create and add professional-looking graphs to your LaTeX documents.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Some frequently asked questions related to graphs in LaTeX are shown below.<\/p>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1630926455237\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q1_Can_LaTeX_draw_graphs\"><\/span>Q1. Can LaTeX draw graphs?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You can draw graphs in LaTeX using TikZ. This refers to a layer over the <em>pgf<\/em> system which enables LaTeX to handle the complex code required in graph creation.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1630926459625\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q2_How_do_I_make_a_simple_graph_in_LaTeX\"><\/span>Q2. How do I make a simple graph in LaTeX?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You can make a simple bullet graphs in latex with four vertices in in LaTeX using the following code:<br \/>\\documentclass[12pt]{article}<br \/>\\usepackage[all]{xy}<br \/>\\begin{document}<br \/>\\xygraph{<br \/>!{&lt;0cm,0cm>;&lt;1cm,0cm>:&lt;0cm,1cm>::}<br \/>!{(0,0) }<em>+ {\\bullet_{a}} !{(0,3) }<\/em>+ {\\bullet_{b}}<br \/>!{(3,1) }<em>+ {\\bullet_{c}} !{(5,-1)}<\/em>+ {\\bullet_{d}}<br \/>}<br \/>\\end{document}<br \/>The vertices in the above code are <em>a<\/em>,<em>b<\/em>,<em>c<\/em>,<em> <\/em>and <em>d<\/em>. They are located at coordinates <em>(0,0)<\/em>, <em>(o,3)<\/em>, <em>(3,1)<\/em>,<em> <\/em>and <em>(5,-1)<\/em> respectively, and are not connected by any lines or edges.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1630926496483\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q3_How_do_you_plot_a_line_in_LaTeX\"><\/span>Q3. How do you plot a line in LaTeX?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p> You can make a simple bullet graphs in latex with four vertices in in LaTeX using the following code <br \/>\\begin{tikzpicture}<br \/>\\begin{axis}[<br \/>xlabel=YOUR XAXIS LABEL,<br \/>ylabel=YOUR YAXIS LABEL,<br \/>width=10cm,height=7cm,<br \/>legend style={at={(0.0,.91)},anchor=west}<br \/>]<br \/>\\addplot[color=red,mark=x] coordinates {<br \/>(1, 5)<br \/>(2, 6)<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"gb-headline gb-headline-c8ee9dc0 gb-headline-text\"><span class=\"ez-toc-section\" id=\"Further_Reading\"><\/span><strong>Further Reading<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>LaTex Tutorial<\/strong><\/p>\n\n\n\n<ol><li><a href=\"https:\/\/www.scijournal.org\/articles\/pros-and-cons-of-using-latex\">27 Pros and Cons of Using LaTex for Scientific Writing<\/a> <\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-document\">6 easy steps to create your first Latex document examples<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/circuit-diagrams-in-latex\">How to add circuit diagrams in Latex<\/a> <\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-font\">How to change Latex font and font size<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-table-of-contents\">How to create a Latex table of contents<\/a> <\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/footnotes-in-latex\">How to create footnotes in LaTeX and how to refer to them, using the builtin commands<\/a> <\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/glossaries-in-latex\">How to create Glossaries in LaTeX<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/plots-in-latex\">How to create plots in Latex \u2013 codes and examples<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/symbols-in-latex\">How to create symbols in LaTeX \u2013 commands for Latex greek alphabet<\/a>\u00a0 <\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-tables\">How to create tables in LaTeX \u2013 rows, columns, pages and landscape tables<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/source-code-in-latex\">How to highlight source code in LaTeX<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/image-in-latex\">How to insert an image in LaTeX \u2013 Managing Latex figure and picture<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-bullet-points\">How to Itemize and Number List \u2013 Adding Latex Bullet Points<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/hyperlinks-in-latex\">How to make hyperlink in latex \u2013 Clickable links<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/reference-in-latex\">How to reference in Latex \u2013 5 steps to bibliography with Bibtex<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-packages\">How to use Latex Packages with examples<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-paragraphs\">How to use LaTeX paragraphs and sections<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-installation-guide\">LaTeX Installation Guide \u2013 Easy to follow steps to install LaTex<\/a><\/li><li><a href=\"https:\/\/www.scijournal.org\/articles\/latex-equations-and-math\">Learn to typeset and align Latex equations and math<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>LaTeX is a versatile document creation system that can handle many complex tasks. Earlier we discussed different ways to create plots in LaTeX. You may be surprised to learn that it is also possible to create highly detailed graphs in LaTeX and customize them to your liking. This guide will show you how to make &#8230; <a title=\"How to Draw Graphs in LaTeX &#8211; Vector Graphics With TikZ\" class=\"read-more\" href=\"https:\/\/www.scijournal.org\/articles\/graphs-in-latex\" aria-label=\"Read more about How to Draw Graphs in LaTeX &#8211; Vector Graphics With TikZ\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":317,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/posts\/316"}],"collection":[{"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":13,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":5586,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/posts\/316\/revisions\/5586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/media\/317"}],"wp:attachment":[{"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scijournal.org\/articles\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}