Skip to content

数据源

本章节介绍通过 \addplot 命令引入的数据源,作为预备知识,请确保已经阅读 AddPlot

函数

该小节仅介绍 PGFplots 内部的计算函数,更多函数请查看:Gnuplot外部程序

x 为变量的函数可写为:

latex
\begin{axis}
  \addplot {x^2};
  \addplot {sin(deg(x))};
\end{axis}

函数表达式支持:

  • 操作符:+ - * / ^ mod < >
  • 数学函数:abs round floor min max
  • 三角函数:sin cos tan atan asin atan cot sec cosec
  • 求值函数:exp ln sqrt factorial (取因子)
  • 进制函数:hex oct bin
  • 角度转换:deg (弧度到角度) rad (角度到弧度)
  • 求随机数:rand (-1 - 1) rnd (0 - 1)
  • 常量:pi e

Options

domain= a:b x 的取值范围,如 domain=-pi:pi

domain y= a:b y 的取值范围

samples at= {a,b,c,d,...} x的取值列表,如:

  • \pgfplotsset{samples at={5e-5,7e-5,10e-5,12e-5}}
  • \pgfplotsset{samples at={-5,-4.5,...,5}}
  • \pgfplotsset{samples at={-5,-3,-1,-0.5,0,...,5}}

samples= 数字 x 的取值数量,当 samples at 已配置时无效

samples y= 数字 y 的取值数量

variable= 字符 表示横轴变量,默认为 x

variable y= 字符 表示纵轴变量,默认为 y

trig format plots= default deg rad 决定坐标轴的单位是弧度还是角度

Table

以表格作为数据,如表格文件 datafile.dat

dof     L2              Lmax            maxlevel
5       8.31160034e-02  1.80007647e-01  2
17      2.54685628e-02  3.75580565e-02  3
49      7.40715288e-03  1.49212716e-02  4
129     2.10192154e-03  4.23330523e-03  5
321     5.87352989e-04  1.30668515e-03  6
769     1.62269942e-04  3.88658098e-04  7
1793    4.44248889e-05  1.12651668e-04  8
4097    1.20714122e-05  3.20339285e-05  9
9217    3.26101452e-06  8.97617707e-06  10

引用该文件:

latex
\begin{tikzpicture}
    \begin{loglogaxis}[
        xlabel=Dof,
        ylabel=$L_2$ error,
    ]
        \addplot table [x=dof,y=L2] {datafile.dat};
    \end{loglogaxis}
\end{tikzpicture}

也可以使用内联的方式,直接写入表格数据:

latex
\addplot table [x=dof,y=Lmax] {
    dof     L2              Lmax            maxlevel
    5       8.31160034e-02  1.80007647e-01  2
    17      2.54685628e-02  3.75580565e-02  3
    49      7.40715288e-03  1.49212716e-02  4
    129     2.10192154e-03  4.23330523e-03  5
    321     5.87352989e-04  1.30668515e-03  6
    769     1.62269942e-04  3.88658098e-04  7
    1793    4.44248889e-05  1.12651668e-04  8
    4097    1.20714122e-05  3.20339285e-05  9
    9217    3.26101452e-06  8.97617707e-06  10
};

如果要多次使用某一文件,可以用 \pgfplotstableread 命令:

latex
\pgfplotstableread{datafile.dat}\customtablename % use any custom name in place of `\customtablename'
...
\addplot table [x=dof,y=L2] {\customtablename};
\addplot table [x=dof,y=Lmax] {\customtablename};

Options

为避免重复内容,以下所有的 x 都可以替换为 yz 来表示对 y轴 和 z轴 的配置。

header= true false 表格是否有 Header, 设置为 false 表示数据从第一行开始读取

x= 列名 x轴数据来源的列名

x index= 列索引 x轴数据来源的列索引,从0开始

x expr= 表达式 x轴数据由表达式生成,表达式可以是:

  • \thisrow{列名} 以某一列的值作为x轴数据,可以添加计算表达式如:\thisrow{column1}+10
  • \thisrowno{列索引} 同上,以列索引为查找对象
  • \coordindex 以行数为x轴数据,行数不计入 header 和被跳过的行(被 comment chars 注释的行)
  • \lineno 同上,但会计入 header 和被跳过的行

x error= 列名 x轴误差数据来源的列名

  • x error <plus|minus> <index|expr>= 列索引|表达式 这里表示多种写法的组合,如x error plus expr= 表达式
  • plus 表示误差上限值 minus 表示下限值,误差线相关知识请查看 误差线

meta <none|index|expr>= 列名|列索引|表达式 点样式列,相关知识请查看 点样式

row sep= newline \ 行的分隔符,默认为 newline 以换行符分隔

col sep= space tab comma colon semicolon braces & ampersand 列的分隔符,默认为 space 以空格分隔

read completely= auto true false 是否始终将整个表读入内存

comment chars= 字符 跳过以该字符开始的行,默认跳过以 #% 开始的行数据

  • 如果想跳过某个单元格,请将该单元格填写为 nan

skip first n= 数字 跳过开头的几行,默认为 0

search path= 路径 表的路径,默认为 . 表示当前目录

Graphics

AddPlot 可以通过 graphics 引用外部图片:

latex
\addplot graphics [
    xmin=-3,xmax=3,
    ymin=-3,ymax=3,
] {external1};

还可以通过配置对坐标轴的点与图中的点进行关联,以让它们组合得更加和谐。

由于这块知识不是本文档的重心,在此不展开。

如感兴趣请自行访问 这里