How to Draw Graphs in LaTeX – Vector Graphics With TikZ

How to Draw Graphs in LaTeX – Vector Graphics With TikZ

This post may contain affiliate links that allow us to earn a commission at no expense to you. Learn more

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 graphs in Latex using the TikZ package and tailor them according to your document requirements. 

How Does LaTeX Create Graphs?

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. 

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.

In the present day, LaTeX creates complex graphs with the help of the pgfplots package. This is a special package based on the TikZ tool, that is widely used to create scientific and technical graphics. 

Understanding How Graphs in latex are Created

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.

For example, let’s 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.

Creating Bullet Graphs in LaTeX

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:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
\xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::} 
!{(0,0) }*+ {\bullet_{a}}
!{(0,3) }*+ {\bullet_{b}} 
!{(3,1) }*+ {\bullet_{c}}  
!{(5,-1)}*+ {\bullet_{d}}  
}  
\end{document} 

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.

In the code !{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}, <0cm,0cm> sets the graph’s origin to the coordinate (0,0). The next two parameters <1cm,0cm> and <0cm,1cm> specify the lengths of the graph’s x and  y axes.

We then added points or “vertices”to our graph using the command !{(…,…) + {\bullet_{YOURLABEL}. In this code, the values inside (…,…) specify the x and  y coordinates for the node, and YOURLABEL indicates the label you wish to give your node. 

Creating a Graph With a Reversed Y-Axis

Now let’s try the same example again, but with a reversed y-axis:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
\xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,-1cm>::} 
!{(0,0) }*+ {\bullet_{a}}
!{(0,3) }*+ {\bullet_{b}} 
!{(3,1) }*+ {\bullet_{c}}  
!{(5,-1)}*+ {\bullet_{d}}  
}  
\end{document}

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 !{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::} from the original graph to !{<0cm,0cm>;<1cm,0cm>:<0cm,-1cm>::}

Creating Graphs With Different Axis Points

It is also possible to create a bullet graph with smaller xy axes scales. For example:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
\xygraph{  
!{<0cm,0cm>;<0.5cm,0cm>:<0cm,0.5cm>::} 
!{(0,0) }*+ {\bullet_{a}}
!{(0,3) }*+ {\bullet_{b}} 
!{(3,1) }*+ {\bullet_{c}}  
!{(5,-1)}*+ {\bullet_{d}}  
}  
\end{document} 

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 !{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::} to !{<0cm,0cm>;<0.5cm,0cm>:<0cm,0.5cm>::} .

Enclosing Graph Points in a Frame

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 \[ \fbox …YOURGRAPHCODE…..\]

For example:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
\[ \fbox{\xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::} 
!{(0,0) }*+ {\bullet_{a}}
!{(0,3) }*+ {\bullet_{b}} 
!{(3,1) }*+ {\bullet_{c}}  
!{(5,-1)}*+ {\bullet_{d}}  
}} \] 
\end{document} 

Creating Directed Graphs in LaTeX

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:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
 \[ \fbox{ \xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}  
!{(0,0) }*+{\bullet_{a}}="a"  
!{(0,3) }*+{\bullet_{b}}="b"  
!{(3,1) }*+{\bullet_{c}}="c"  
!{(5,-1)}*+{\bullet_{d}}="d"  
"a"-"b" "d"-"a"  
"b"-"c"  
"c"-"d"  
"d"-"c"  
} } \]  
  
\end{document} 

In the above example, the code “a”-“b” “d”-“a”  “b”-“c”  “c”-“d”  “d”-“c” describes the path connecting various points. So the path that connects point a with point b is represented as “a”-“b” in the code.

Creating Directed Graphs in Latex With Arrows

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:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
 \[ \fbox{ \xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}  
!{(0,0) }*+{\bullet_{a}}="a"  
!{(0,3) }*+{\bullet_{b}}="b"  
!{(3,1) }*+{\bullet_{c}}="c"  
!{(5,-1)}*+{\bullet_{d}}="d"  
"a":"b" "d":"a"  
"b":"c"  
"c":"d"  
"d":"c"  
} } \]  
  
\end{document} 

The new graph shows arrows directing from a to b, b to c, d to c, a to d, and d to a. Notice that we simply swapped out the “” in the earlier code with “:” to achieve this. When used properly, you can create an intricate direction path between vertices for your graphs in LaTeX

Drawing Bent Edges Between Vertices

If you wish to create the graphs in latex with vertices connected via bending edges, you must use “-@/^/” between the vertices you are specifying. If we apply this to the above example, the code becomes:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
 \[ \fbox{ \xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}  
!{(0,0) }*+{\bullet_{a}}="a"  
!{(0,3) }*+{\bullet_{b}}="b"  
!{(3,1) }*+{\bullet_{c}}="c"  
!{(5,-1)}*+{\bullet_{d}}="d"  
"a":"b" "d":"a"  
"b":"c"  
"c"-@/^/"d"  
"d"-@/^/"c"  
} } \]  

The new graph shows two bent lines connecting vertices c and d, while the other lines are straight directional ones with arrows.

It is also possible to set the length of the bending edge using the command -@/^…cm/ .  We demonstrate this in the following example:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
  
\begin{document}  
  
 \[ \fbox{ \xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}  
!{(0,0) }*+{\bullet_{a}}="a"  
!{(2,3) }*+{\bullet_{b}}="b"  
!{(3,1) }*+{\bullet_{c}}="c"  
!{(2,-1)}*+{\bullet_{d}}="d"  
"a"-"b"  
"b"-@/^3cm/"d"  
"d"-"c"  
"a"-@/^/"d"  
"b"-@/^2cm/"c" 
} } \]  

The above command sets the length of the bent edge connecting b and d to 3cm, and the one connecting b and c 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.

Adding Labels to Edges in Graphs

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:

"a"-"b"^(0.5)(h_1)  
"b"-@/^3cm/"d"(0.5)(i_2)
"d"-"c"^(0.4)(j_3)  
"a"-@/^/"d"^(0.6)(k_5)  
"b"-@/^2cm/"c"^(0.4)(i_4)
} } \]  

In the above code, “a”-“b”^(0.5)(h_1) sets the label h_1 a distance of 0.5 from the a-b edge. 

Similarly, “b”-@/^3cm/”d”(0.5)(i_2) sets the label i_2 a distance of 0.5 from the b-d edge.

“d”-“c”^(0.4)(j_3) sets the label j_3 a distance of 0.4 from the d-c edge.

“a”-@/^/”d”^(0.6)(k_5) sets the label k_5 a distance of 0.6 from the a-d edge.

“b”-@/^2cm/”c”^(0.4)(i_4) sets the label i_4 a distance of 0.4 from the b-c edge.

Adding Colored Vertices to Graphs

LaTeX also allows you to create graphs with colored vertices. You can do this by adding the color name before the command \bullet. However, you will also need to use the package color to add colors to your graph. An example of this code is shown below:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
\usepackage{xcolor}  
\begin{document}  
  
 \[ \fbox{ \xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}  
!{(0,0) }*+[green]{\bullet_{a}}="a"  
!{(0,3) }*+{\bullet_{b}}="b"  
!{(3,1) }*+[blue]{\bullet_{c}}="c"  
!{(5,-1)}*+[red]{\bullet_{d}}="d"  
"a":"b" "d":"a"  
"b":"c"  
"c"-@/^/"d"  
"d"-@/^/"c"  
} } \]  

The above code assigns a red vertice for a, a blue vertice for b, and a red vertice for d. The vertice for c remains black as we did not specify a color for it.

Adding Colored Edges to Graphs

The process of adding colored edges to graphs is similar to the above. For example:

\documentclass[12pt]{article}  
\usepackage[all]{xy}  
\usepackage{xcolor}  
\begin{document}  
  
 \[ \fbox{ \xygraph{  
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}  
!{(0,0) }*+{\bullet_{a}}="a"  
!{(0,3) }*+{\bullet_{b}}="b"  
!{(3,1) }*+{\bullet_{c}}="c"  
!{(5,-1)}*+{\bullet_{d}}="d"  
"a":"b" "d":[green]"a"  
"b":"c"  
"b"-@/^/[orange]"d"  
"d"-@/^/[red]"c"  
} } \]  

The above code adds a green edge between d and a, an orange edge between d and c, and a red edge between b and d.

Creating Bar Graphs in LaTeX

LaTeX allows you to create detailed bar graphs in latex thanks to pgfplots. These bar graphs are highly customizable, so you can tailor them to your liking in many ways. Let’s start with the example code below:

\begin{tikzpicture}
\begin{axis}[
	x tick label style={
		/pgf/number format/1000 sep=},
	ylabel=Pounds of Fish Caught,
	enlargelimits=0.06,
	legend style={at={(0.5,-0.1)},
	anchor=north,legend columns=-1},
	ybar interval=0.7,
]
\addplot 
	coordinates {(2010,2313) (2009,3872)
		 (2008,1758) (2007,3257)};
\addplot 
	coordinates {(2010,1783) (2009,2306) 
		(2008,1854) (207,3717)};
\legend{Ship1,Ship2}
\end{axis}
\end{tikzpicture}

In the above code, we began our code with the command \begin{tikzpicture} to create a tikzpicture environment. This environment is necessary for pgfplots to load.

Next we defined the plot style using the code: 

\begin{axis}[
	x tick label style={
		/pgf/number format/1000 sep=}

The code enlargelimits=0.06 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 0.06 is relative to the bar plot’s total height.

The code legend style={at={(0.5,-0.1)}, anchor=north,legend columns=-1} sets the legend’s style. Changing the second parameter -0.1 in this code would move the legend closer or farther from the x-axis.

The code ybar interval=0.7 sets the thickness of the bars. Setting this to 1 sets the bars directly next to each other and with no gap. Setting this to 0 makes the bars so narrow they appear as lines.

We then added bar plots for each series. For Ship1 we used the code 

\addplot 
	coordinates {(2010,2313) (2009,3872)
		 (2008,1758) (2007,3257)};

The above coordinates sets a bar chart with a height of 2313 in 2010, 3872 in 2009, and 1758 in 2008.

Final Thoughts

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. 

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.

Frequently Asked Questions

Some frequently asked questions related to graphs in LaTeX are shown below.

Q1. Can LaTeX draw graphs?

You can draw graphs in LaTeX using TikZ. This refers to a layer over the pgf system which enables LaTeX to handle the complex code required in graph creation.

Q2. How do I make a simple graph in LaTeX?

You can make a simple bullet graphs in latex with four vertices in in LaTeX using the following code:
\documentclass[12pt]{article}
\usepackage[all]{xy}
\begin{document}
\xygraph{
!{<0cm,0cm>;<1cm,0cm>:<0cm,1cm>::}
!{(0,0) }+ {\bullet_{a}} !{(0,3) }+ {\bullet_{b}}
!{(3,1) }+ {\bullet_{c}} !{(5,-1)}+ {\bullet_{d}}
}
\end{document}
The vertices in the above code are a,b,c, and d. They are located at coordinates (0,0), (o,3), (3,1), and (5,-1) respectively, and are not connected by any lines or edges.

Q3. How do you plot a line in LaTeX?

You can make a simple bullet graphs in latex with four vertices in in LaTeX using the following code
\begin{tikzpicture}
\begin{axis}[
xlabel=YOUR XAXIS LABEL,
ylabel=YOUR YAXIS LABEL,
width=10cm,height=7cm,
legend style={at={(0.0,.91)},anchor=west}
]
\addplot[color=red,mark=x] coordinates {
(1, 5)
(2, 6)

Further Reading

LaTex Tutorial

  1. 27 Pros and Cons of Using LaTex for Scientific Writing
  2. 6 easy steps to create your first Latex document examples
  3. How to add circuit diagrams in Latex
  4. How to change Latex font and font size
  5. How to create a Latex table of contents
  6. How to create footnotes in LaTeX and how to refer to them, using the builtin commands
  7. How to create Glossaries in LaTeX
  8. How to create plots in Latex – codes and examples
  9. How to create symbols in LaTeX – commands for Latex greek alphabet 
  10. How to create tables in LaTeX – rows, columns, pages and landscape tables
  11. How to highlight source code in LaTeX
  12. How to insert an image in LaTeX – Managing Latex figure and picture
  13. How to Itemize and Number List – Adding Latex Bullet Points
  14. How to make hyperlink in latex – Clickable links
  15. How to reference in Latex – 5 steps to bibliography with Bibtex
  16. How to use Latex Packages with examples
  17. How to use LaTeX paragraphs and sections
  18. LaTeX Installation Guide – Easy to follow steps to install LaTex
  19. Learn to typeset and align Latex equations and math

Photo of author
Author
SJ Tsai
Chief Editor. Writer wrangler. Research guru. Three years at scijournal. Hails from a family with five PhDs. When not shaping content, creates art. Peek at the collection on Etsy. For thoughts and updates, hit up Twitter.

Leave a Comment