用 CSV 文件中的数据绘图
把文件添加到你的项目中,然后在 pgfplots 的 axis 中写 \addplot table[x=col1, y=col2, col sep=comma] {file.csv};。
绘制文件中的两列
把 CSV 文件上传到你的项目中(或在项目中新建)。第一行是列名。x= 和 y= 按名称选择列,col sep=comma 说明数值之间用逗号分隔。
本页使用一个小文件 data-from-csv-weather.csv:
month,temp,rain,sd
1,1.2,44,0.8
2,2.0,38,0.7
3,5.6,41,0.9
...
12,2.3,46,0.8来自 CSV 文件的每月气温
\begin{tikzpicture}
\begin{axis}[xlabel={Month}, ylabel={Temperature ($^\circ$C)}, xtick={1,...,12}]
\addplot table[x=month, y=temp, col sep=comma] {data-from-csv-weather.csv};
\end{axis}
\end{tikzpicture}文件夹中的文件也可以使用:按项目中的路径书写,例如 {data/weather.csv}。
多列、误差列和计算值
每个系列读取一次同一个文件。y error= 从某一列获取误差棒,y expr= 根据该行计算一个值,用 \thisrow{name} 引用某一列。
来自同一文件的两个系列
\begin{tikzpicture}
\begin{axis}[xlabel={Month}, ylabel={Value}, legend pos=north west, ymin=0]
\addplot+[error bars/.cd, y dir=both, y explicit]
table[x=month, y=temp, y error=sd, col sep=comma] {data-from-csv-weather.csv};
\addplot+[mark=none, dashed]
table[x=month, y expr=\thisrow{rain}/2, col sep=comma] {data-from-csv-weather.csv};
\legend{Temperature ($^\circ$C), Rain / 2 (mm)}
\end{axis}
\end{tikzpicture}只读取一次表格
\pgfplotstableread 把文件读入一个宏,之后每个 \addplot 都可以使用它。当同一张表格用于多个图表时,这样写更简短。
\pgfplotstableread 和两个图表
\pgfplotstableread[col sep=comma]{data-from-csv-weather.csv}\weather
\begin{tikzpicture}
\begin{axis}[ybar, bar width=6pt, ymin=0, xlabel={Month}, ylabel={Rain (mm)},
xtick={1,...,12}, enlarge x limits=0.06]
\addplot table[x=month, y=rain] {\weather};
\end{axis}
\end{tikzpicture}文档中的数据:filecontents
小型数据集可以直接放在 .tex 文件里。filecontents* 把其内容写成一个以该名称命名的文件,然后由 \addplot table 读取。它位于导言区,在 \begin{document} 之前。
用 filecontents* 写入的数据
\documentclass{article}
\usepackage{pgfplots}
\begin{filecontents*}{speed.dat}
t v
0 0
1 4.4
2 8.1
3 11.0
4 13.2
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={Time (s)}, ylabel={Speed (m/s)}]
\addplot table[x=t, y=v] {speed.dat};
\end{axis}
\end{tikzpicture}
\end{document}这里的列用空格分隔,这是 pgfplots 的默认方式,所以不需要 col sep。
表格选项
| 选项 | 作用 | 在 Scissor LaTeX 中 |
|---|---|---|
x=name、y=name | 按名称选择列 | 支持 |
x index=0、y index=1 | 按编号选择列,从 0 开始 | 支持 |
col sep=comma | 还有 space、tab、semicolon、& | 支持 |
header=false | 第一行是数据,而不是列名 | 支持 |
skip first n=2 | 忽略开头的几行 | 支持 |
x expr=、y expr= | 计算一个值,可用 \thisrow{name}、\thisrowno{n} 或 \coordindex | 支持 |
y error=、x error= | 误差棒所在的列 | 支持 |
comment chars=! | 更多注释字符(以 # 和 % 开头的行总是被跳过) | 支持 |
\pgfplotstableread | 把表格读入宏 | 支持 |
\pgfplotstabletypeset | 把表格输出为 LaTeX 表格 | 不支持:会被跳过并给出提示 |
空单元格、nan 和 inf 被视为缺失值并略去。
出现问题时
如果读取 .csv 文件时没有写 col sep=comma,Scissor LaTeX 仍会找到逗号,所以图表照样绘制,但“问题”标签页会提醒你加上这个选项。TeX 发行版不会自行猜测,图表在那里会失败。
缺少 col sep:照样绘制,并给出提醒
\begin{tikzpicture}
\begin{axis}[width=6cm, height=4.5cm]
\addplot table[x=month, y=temp] {data-from-csv-weather.csv};
\end{axis}
\end{tikzpicture}“问题”标签页: Add col sep=comma to read data-from-csv-weather.csv so it also works in LaTeX
如果文件不在项目中,该图会被略去。坐标轴仍会绘制,“问题”标签页会指出缺少的文件。
缺少的文件
\begin{tikzpicture}
\begin{axis}[width=6cm, height=4.5cm]
\addplot table[col sep=comma] {results.csv};
\end{axis}
\end{tikzpicture}“问题”标签页: Data file not in the project: results.csv
与 TeX 发行版的不同之处
这里的所有内容都是 pgfplots 语法,所以这些文件在 TeX 发行版中也能编译。手册:ctan.org/pkg/pgfplots。来自列的误差棒请参阅误差棒。
常见问题
如何在 LaTeX 中绘制 CSV 文件?
把文件放进项目,然后在坐标轴中写 \addplot table[x=a, y=b, col sep=comma] {file.csv};,把 a 和 b 换成你的列名。
我的 CSV 用分号分隔,该怎么写?
使用 col sep=semicolon。制表符分隔则用 col sep=tab。
可以用行号作为横坐标绘制某一列吗?
可以:table[x expr=\coordindex, y=name] 使用 0、1、2 等作为 x 值。
本页的每个示例都在构建网站时由 Scissor LaTeX 排版。 最后更新:2026-11-07