tikz:从 csv 文件加载数据范围

tikz:从 csv 文件加载数据范围

使用此代码,我可以从 .csv 文件绘图

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=0]
\addplot +[] table[only marks,x=A,y=C, col sep=comma]{imported.csv};
\end{axis}
\end{tikzpicture}
\end{document}

\end{document}

我的.csv 文件内容

A, B, C
0, 14.50, 14.50
1, 14.45, 14.31
2, 14.40, 14.12
3, 14.35, 13.93
4, 14.30, 13.75
5, 14.25, 13.57
6, 14.20, 13.40
7, 14.15, 13.22
8, 14.10, 13.06
9, 14.05, 12.89
10, 14.00, 12.73
11, 13.95, 12.57
12, 13.90, 12.41
13, 13.85, 12.26
14, 13.80, 12.11
15, 13.75, 11.96
16, 13.70, 11.81
17, 13.65, 11.67
18, 13.60, 11.53
19, 13.55, 11.39
20, 13.50, 11.25
21, 13.45, 11.12
22, 13.40, 10.98
23, 13.35, 10.85
24, 13.30, 10.73
25, 13.25, 10.60
26, 13.20, 10.48
27, 13.50, 10.63
28, 13.80, 10.78
29, 14.10, 10.93
30, 14.40, 11.08
31, 14.70, 11.22
32, 15.00, 11.36
33, 15.30, 11.50
34, 15.60, 11.64
35, 15.90, 11.78
36, 16.20, 11.91
37, 16.50, 12.04
38, 16.80, 12.17
39, 17.10, 12.30
40, 17.40, 12.43
41, 17.70, 12.55
42, 18.00, 12.68
43, 18.30, 12.80
44, 18.60, 12.92
45, 18.90, 13.03
46, 19.20, 13.15
47, 19.50, 13.27
48, 19.80, 13.38
49, 20.10, 13.49
50, 20.40, 13.60

我可以仅绘制 .csv 文件中的特定数据范围(例如从“A”列的 0 到 26)吗?

答案1

pgfplots有多种过滤数据的方法,请参阅第 4.22 节跳过或更改坐标 – 过滤器在手册中pgfplots。在这种情况下,您可以使用restrict x to domain=0:26。它基本上按照它所说的那样,只绘制 x 在给定域中的点。

另一种方法允许您使用表中的任何列进行过滤,使用方式x filter如下:

\addplot +[x filter/.expression={and(\thisrow{A}>=0, \thisrow{A}<=26) ? x : nan}] table[only marks,x=A,y=C]{\mydata};

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep=comma]{
A, B, C
0, 14.50, 14.50
1, 14.45, 14.31
2, 14.40, 14.12
3, 14.35, 13.93
4, 14.30, 13.75
5, 14.25, 13.57
6, 14.20, 13.40
7, 14.15, 13.22
8, 14.10, 13.06
9, 14.05, 12.89
10, 14.00, 12.73
11, 13.95, 12.57
12, 13.90, 12.41
13, 13.85, 12.26
14, 13.80, 12.11
15, 13.75, 11.96
16, 13.70, 11.81
17, 13.65, 11.67
18, 13.60, 11.53
19, 13.55, 11.39
20, 13.50, 11.25
21, 13.45, 11.12
22, 13.40, 10.98
23, 13.35, 10.85
24, 13.30, 10.73
25, 13.25, 10.60
26, 13.20, 10.48
27, 13.50, 10.63
28, 13.80, 10.78
29, 14.10, 10.93
30, 14.40, 11.08
31, 14.70, 11.22
32, 15.00, 11.36
33, 15.30, 11.50
34, 15.60, 11.64
35, 15.90, 11.78
36, 16.20, 11.91
37, 16.50, 12.04
38, 16.80, 12.17
39, 17.10, 12.30
40, 17.40, 12.43
41, 17.70, 12.55
42, 18.00, 12.68
43, 18.30, 12.80
44, 18.60, 12.92
45, 18.90, 13.03
46, 19.20, 13.15
47, 19.50, 13.27
48, 19.80, 13.38
49, 20.10, 13.49
50, 20.40, 13.60
}\mydata
\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=0]
\addplot +[restrict x to domain=0:26] table[only marks,x=A,y=C]{\mydata};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容