# How to a plot a function and data in LaTeX

In this tutorial, We will learn how to plot explicit functions from its mathematical expression or from a given data. At the end of this step by step tutorial, we will be able to create axis, add labels and legend and change the graph style. We will be also able to plot data from a multi-column files, All of these using TikZ and Pgfplots packages.

Plotting a function in $\LaTeX$ is quite easy. Let us consider the following function: $$y = \exp(-\frac{x}{10})\cdot (\cos{x}+\frac{1}{10}\cdot \sin{x})$$ where we would like to get something similar to the next figure together with a data plotted from a given file. The first step is to start by setting up the tikzpicture environment.

## 1. Create tikzpicture environment

First of all, we need to set up the project by loading the $\verb|tikz|$ and $\verb|pgfplots|$ packages in the preamble.
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[]
% here comes the code
\end{axis}
\end{tikzpicture}
\end{document}


It is important to use the command $\verb|\pgfplotsset{compat = newest}|$ to specify to the compiler that we are working with the last version of the $\verb|\pgfplots|$ package. In order to start drawing in TikZ we need to use the $\verb|axis|$ environment within the $\verb|tikzpicture|$ environment. All the TikZ commands must be inside the $\verb|axis|$ environment.

## 2. Plot a function in $\LaTeX$

To plot a function, we just need to use the command $\verb|\addplot[options] {expression}|$. Check the following code to figure out how this command should be used for the above function.
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot[] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/10 )};
\end{axis}
\end{tikzpicture}
\end{document}

The domain and range of the plot is auto determinate by the compiler. So if we want to change the limits of the plot we need to specify it manually in the $\verb|axis|$ environment. For this purpose we can use these options:
• $\verb|xmin|$: Lower limit in the $x$ axis for the plot.
• $\verb|xmax|$: Upper limit in the $x$ axis for the plot.
• $\verb|ymin|$: Lower limit in the $y$ axis for the plot.
• $\verb|ymax|$: Upper limit in the $y$ axis for the plot.
For this example let consider $\verb|xmin = 0.0|$, $\verb|xmax = 30|$, $\verb|ymin = -1.5|$ and $\verb|ymax = 2.0|$. Of course you can change these values depending of the domain and range of the function.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
]
domain = 0:30,
] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/10 )};
\end{axis}
\end{tikzpicture}
\end{document}


Don’t forget to specify the domain of the function with the option $\verb|domain = a:b|$. In this case we set this parameter to $\verb|domain = 0:30|$. The domain of the function is independent of the limits of the axis, but usually we take the same values so the plot fills the axis.

## 3. How to make the plot smooth in $\LaTeX$

One can remark that the previous figure corresponds to a rough plot, it’ better to have a smooth one. We can achieve this using these options:
• $\verb|samples|$: This parameter determines the number of point to be plotted for the function, while bigger the number better looks the function.
• $\verb|smooth|$: If we use this option, the compiler makes an interpolation between the point plotted to get a soft appearance for the function.
The obtained illustration is shown below (smooth plot). At that time, we have changed the stroke and color of the function using the $\verb|thick|$ and $\verb|blue|$ options. You can try different strokes: $\verb|ultra thin|$, $\verb|thin|$, $\verb|thick|$ and $\verb|ultra thick|$.
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
]
domain = 0:30,
samples = 200,
smooth,
thick,
blue,
] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/10 )};
\end{axis}
\end{tikzpicture}
\end{document}


## 4. How to change figure size in $\LaTeX$

The next step is to set up the grid and the aspect ratio. To draw a reference grid we can use the next options:

• $\verb|grid|$: When this options is set to $\verb|both|$ the minor and major grid are plotted.
• $\verb|xtick distance|$: Is the distance between major ticks in the $x$ axis.
• $\verb|ytick distance|$: Is the distance between major ticks in the $y$ axis.
• $\verb|minor|$: Is the number of ticks between major ticks.
• $\verb|grid style|$: This command can be used for the major and minor grid and change the colour and stroke of the grid.

To change the aspect ratio of the figure we can set the $\verb|width|$ and $\verb|height|$ parameters to specific values. It is very common to use $\verb|width = \textwidth|$ and $\verb|height = 0.5\textwidth|$, but it always depend of the aspect you want to give to the figure. In some cases it’s better to have a landscape aspect ratio rather than a portrait aspect ratio (check above the obtained results ).

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
xtick distance = 2.5,
ytick distance = 0.5,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
]
domain = 0:30,
samples = 200,
smooth,
thick,
blue,
] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/10 )};
\end{axis}
\end{tikzpicture}
\end{document}


## 5. How to plot data from a file in $\LaTeX$

Now suppose you don’t have the expression of the function you want to plot, but instead you have a file, for example $\verb|cosine.dat|$ with the coordinates as shwon below. Here we use a space as separator of the coordinates, but also you can use a comma, only specifies the separator using the option $\verb|col sep = comma|$ in the $\verb|\addplot|$ command. Plotting a data files is very similar to plot a mathematical expression, we use again the $\verb|\addplot|$ command but in slightly different way:

$\verb|\addplot[options] file[options] {file_name.dat}|$

 x y
0.0 1.0000
0.5 0.8776
1.0 0.5403
1.5 0.0707
2.0 -0.4161
2.5 -0.8011
3.0 -0.9900
3.5 -0.9365
4.0 -0.6536
4.5 -0.2108
5.0 0.2837
5.5 0.7087
6.0 0.9602
6.5 0.9766
7.0 0.7539
7.5 0.3466
... % data file

The next code shows the implementation of this command for plot of the data file.
\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
xtick distance = 2.5,
ytick distance = 0.5,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
xlabel = {$x$},
ylabel = {$y$},
]
domain = 0:30,
samples = 200,
smooth,
thick,
blue,
] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/10 )};

smooth,
thin,
red,
dashed
] file[skip first] {cosine.dat};
\end{axis}
\end{tikzpicture}
\end{document}
Now we have the file plotted, which in this case represents the cosine function, but you can plot any set of coordinates. In the options we have added the $\verb|dashed|$ option with a red colour. Also notice that we have used the option $\verb|skip first|$ since the data file have labels instead of numbers in their first line. For labels, we have added the following keys:
• $\verb|xlabel|$: Show the name of the horizontal axis.
• $\verb|ylabel|$: Show the name of the vertical axis.
We are almost done, we just need to add a legend to the graphic. This can be done simply by adding the $\verb|\legend{labels}|$ command after the last $\verb|\addplot|$. By doing so, we will get the figure in question.

## 6. Plot data from a multicolumn file in $\LaTeX$

Now suppose you want to plot a data file with several columns, for example these file named $\verb|multiple_functions.dat|$, which have multiple columns.
 x	        y1	        y2	        y3
0.0     0.0000	    0.0000	    0.0000
1.0     0.0100	    0.0050	    0.0025
2.0     0.0400	    0.0200	    0.0100
3.0     0.0900	    0.0450	    0.0225
4.0     0.1600	    0.0800	    0.0400
5.0     0.2500	    0.1250	    0.0625
6.0     0.3600	    0.1800	    0.0900
7.0     0.4900	    0.2450	    0.1225
8.0     0.6400	    0.3200	    0.1600
9.0     0.8100	    0.4050	    0.2025
10.0	   1.0000	    0.5000	    0.2500 % data file

In this data file we can see four columns, the first one is the coordinates of the $x$ axis and the other three columns corresponds to the $y$ axis values for each $x$ value. It means we need to plot three functions from a single data file. This can be achieved very easy by using the command: $\verb|\pgfplotstableread{file.dat}{\table}|$ This command reads a file and saves it as a table where you can access the columns once by once. The next code shows the implementation of the $\verb|\pgfplotstableread|$ command in order to plot the data file. To plot a specific column from a data file, we can use the $\verb|\addplot|$ along with the $\verb|table|$ command, as follows: \addplot[options] table [x = {column_x}, y = {column_y}] {\table}; With the table option you can specify the name of the column you want to be in the $x$ axis and the name of the column you want to be in the $y$ axis. Let’s consider different plot style, one with marks and lines, and two only with marks. The options available for a plot with marks are:
• $\verb|mark|$: This adds a mark for every coordinate plotted, and it can be equal to $\verb|*|$, $\verb|o|$ or $\verb|x|$, depending of the style you want for the mark.
• $\verb|only marks|$: Plots only the marks without the connecting lines.
• $\verb|mark size|$: Specifies the size of the marks.
 \documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 10,
ymin = 0, ymax = 1,
xtick distance = 1,
ytick distance = 0.25,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.75\textwidth,
legend cell align = {left},
legend pos = north west
]
\addplot[blue, mark = *] table [x = {x}, y = {y1}] {\table};
\addplot[red, only marks] table [x ={x}, y = {y2}] {\table};
\addplot[teal, only marks, mark = x, mark size = 3pt]
table [x = {x}, y = {y3}] {\table};
\legend{
Plot with marks and line,
Plot only with marks,
Plot with other type of marks}
\end{axis}
\end{tikzpicture}
\end{document}


This can be useful when you have to plot several graphics depending from one variable. We can find this data files from experiments or from databases information and plot them for better visualisation.

With the method described in this document you are able to draw any explicit functional equation or any set of coordinates. The $\verb|pgfplots|$ package allows to plot any number of functions in the same figure. Now it’s up to you how to use this tools to create your own graphics.