How to use PgfPlots in latex

本文介绍使用pgfplots来进行绘图的一些方法,日后会随用随更新用例。官方的参考文档:

Pgfplot manual

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:

1
2
3
\usepackage{tikz} % To generate the plot from csv
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}

使用 \addplot table 来引入数据绘图

例如 data.dat:

1
2
3
4
5
6
7
8
9
10
x	f(x)
3.16693000e-05 -4.00001451e+00
1.00816962e-03 -3.08781504e+00
1.98466995e-03 -2.88058811e+00
2.96117027e-03 -2.75205040e+00
3.93767059e-03 -2.65736805e+00
4.91417091e-03 -2.58181091e+00
5.89067124e-03 -2.51862689e+00
6.86717156e-03 -2.46413745e+00
....

Using the following commands can generate the plots automatically.

1
2
3
4
5
6
7
8
9
10
11
\begin{tikzpicture}
\begin{axis}[
title = {Picture 1}, % whatever name you want
xlabel = {$x$},
ylabel = {$y$},
ymin = -3, ymax = 3,
minor y tick num = 1,
]
\addplot[blue] table {data.dat};
\end{axis}
\end{tikzpicture}
  • \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 keyword table also accepts an option list (for example, to choose columns, to define a different col sep or row sep or to provide some math expression which is applied to each row).

Dealing with multiple files simultaneously

1
2
3
4
5
6
7
8
9
10
11
12
13
\begin{tikzpicture}
\begin{loglogaxis}[
title = {Convergence Plot},
xlabel = {Degrees of freedom},
ylabel = {$L_2$ Error},
grid = major,
legend entries = {$d=2$, $d=3$, $d = 4$},
]
\addplot table {data_d2.dat};
\addplot table {data_d3.dat};
\addplot table {data_d4.dat};
\end{loglogaxis}
\end{tikzpicture}

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

使用.csv文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\begin{tikzpicture}[scale=.8],
\begin{axis}[
legend entries={Online market share \%,Total market share \%},
xtick={2018,2019,2020,2021,2022,2023},
xticklabels = {2018,2019,2020,2021e,2022e,2023e},
ytick = {0,10,20,30},
ymin=-1,
ymax=35,
xlabel = Year,
ylabel = Market Scale(bn yuan),
nodes near coords,
nodes near coords align = {vertical},
table/col sep=comma,
legend style={
at={(0.75,0.95)},
},
]
\addplot table[x=year,y=online] {figure/market_scale.csv};
\addplot table[x=year,y=total] {figure/market_scale.csv};

\end{axis}
\end{tikzpicture}

在axis中添加参数: table/col sep = comma

或者在add plot中添加参数:

\addplot table[x=year,y=online, col sep = comma] {figure/market_scale.csv};

使用 \addplot expression 来绘制函数

1
2
3
4
5
6
7
8
9
10
11
12
13
\begin{tikzpicture}
\begin{axis}
% density of Normal distribution:
\newcommand\MU{0}
\newcommand\SIGMA{1e-3}
\addplot[
red,
domain = -3*\SIGMA: 3*\SIGMA,
samples = 201,
]
{exp(-(x-\MU)^2 / 2 / \SIGMA^2) / (\SIGMA * sqrt(2*pi))};
\end{axis}
\end{tikzpicture}

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.

绘制柱状图

分栏双图

legend

坐标刻度设定