How to use pgfplots in Latex
This article introduces a few commonly used methods for plotting in latex. In this way, there is no need to save picture and include it. Every time when you change your numerical experiment, to update the plot, just need to update the data file.
Before we start, the following packages and command are useful:
Plotting data from .dat
files in LaTeX
Using \addplot table
Ploting data directly from a .dat
file.
For example, if a file called data.dat
has the data table shown as follows:
Using the following commands can generate the plots automatically.
Then the corresonding plot will be:
REMARK:
\begin{axis}... \end{axis}
is the environment for a normal axis.- Axis descriptions can be added using the keys
title
,xlabel
,ylabel
as we have in our example listing. - The keys
ymin
,ymax
,xmin
,xmax
control only the visible part, i.e., the axis range. - The third new option is
minor y tick num = 1
which allows to customize minor ticks. \addplot[blue]
means that the plot will be placed in blue color,\addplot table
loads a table from a file and plots the first two columns. The keywordtable
also accepts an option list (for example, to choose columns, to define a differentcol sep
orrow sep
or to provide some math expression which is applied to each row).
Using \addplot expression
Of course, we can also use another way to generate a plot.
More precisely, \addplot expression
can make it.
For example:
The above commands can obtain the following plot:
REMARK:
domain
defines the sampling interval in the form a:b.samples = N
expects the number of samples inserted into the sampling interval.- Here the
expression
is the density of Normal distribution.
Dealing with multiple files simultaneously
We can read several data files at the same time and then plot them into one picture. For example, if we have three data files, each of them corresponds to one parameter d, which could be 2, 3 or 4. The data file for d = 2 is stored in data_d2.dat and it contains the following data:
The other files are similar.
If we wnat to produce a loglog plot, the following commands can make this happen:
Here is the result:
REMARK:
- We used
\begin{loglogaxis}
instead of\begin{axis}
in order to configure logarithmic scales on both axes. - The plot contains grid lines since we used the
grid = major
key. - A legend can be provided for one or more
\addplot
statements using thelegend entries
key. pgfplots
accepts more than one\addplot ... ;
command, so we can just add our remaining data files.- We do not need to worry about the line styles, since
pgfplots
will automatically choose styles for that specific plot.
3D Scatter Plots
This is similar to two dimensional plot.
For example, if we have a file called data_3d.dat
which contains the following data:
Using the below commands can obtain the desired plot:
REMARK:
- The
\addplot3
command is the main interface for any three dimensional plot. only marks
places the current plot mark at each input position.- If we add the key
scatter
, the plot mark will also use the colors of the currentcolormap
.
Plotting data from .csv
files in LaTeX
Similar to .dat
file, we can also use \addplot table
to import data from .csv
file.
Let’s use the previos skills to deal with a relatively more complex example.
Here is data from data.csv
file:
people | probability |
---|---|
0 | 0.000000 |
1 | 0.000000 |
2 | 0.002740 |
3 | 0.008204 |
4 | 0.016356 |
5 | 0.027136 |
… | … |
Then the following commands can help us obtain the below plot:
For more details about the package pgfplots
, please download the pdf tutorial Manual for Package PGFPLOTS as a reference.