1

With this code, I can plot from .csv file

\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}

And my .csv file content

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

Can I plot only a specific data range (e.g. from 0 to 26 from the "A" column) from .csv file?

2
  • Please, give a fully compilable code.
    – AndréC
    Jul 17, 2020 at 16:47
  • Sorry, I'll do it right away Jul 17, 2020 at 17:00

1 Answer 1

1

pgfplots has various ways of filtering the data, see section 4.22 Skipping Or Changing Coordinates – Filters in the pgfplots manual. In this case you can use restrict x to domain=0:26. It does basically what it says, plotting only the points where x is in the given domain.

Another approach, which lets you use any column in the table for the filter, is to use x filter like this:

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

enter image description here

\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}
1
  • Thank you very much! Jul 17, 2020 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.