Data from Files in PGFPlots
Import and visualize data from external files like CSV, TSV, and plain text.
Basic File Import
Given a file data.txt with space-separated values:
# data.txt
x y
0 0
1 1
2 4
3 9
4 16\begin{axis}
\addplot table {data.txt};
\end{axis}CSV Files
# data.csv
time,value,error
0,1.2,0.1
1,2.5,0.2
2,3.8,0.15
3,5.1,0.25\begin{axis}[
xlabel={Time},
ylabel={Value}
]
\addplot table[
col sep=comma, % Comma-separated
x=time, % Column name for x
y=value % Column name for y
] {data.csv};
\end{axis}Column Selection
\begin{axis}
% By column name
\addplot table[x=time, y=value] {data.csv};
% By column index (0-based)
\addplot table[x index=0, y index=1] {data.txt};
% Using expressions
\addplot table[
x=time,
y expr=\thisrow{value}*2 % Double the y values
] {data.csv};
\end{axis}Error Bars from File
\begin{axis}
\addplot[
mark=*,
error bars/.cd,
y dir=both,
y explicit
] table[
x=time,
y=value,
y error=error, % Error column
col sep=comma
] {data.csv};
\end{axis}Tab-Separated Files
\begin{axis}
\addplot table[
col sep=tab % Tab-separated
] {data.tsv};
\end{axis}Skipping Header Rows
\begin{axis}
\addplot table[
skip first n=2, % Skip first 2 rows
% or
ignore chars={\#}, % Ignore lines starting with #
comment chars={\%} % Treat % as comment
] {data.txt};
\end{axis}Inline Data
\begin{axis}
\addplot table {
x y
0 0
1 1
2 4
3 9
4 16
};
\end{axis}
% Or using filecontents
\begin{filecontents}{mydata.dat}
x y
0 1
1 2
2 4
\end{filecontents}
\begin{axis}
\addplot table {mydata.dat};
\end{axis}Mathematical Operations on Data
\begin{axis}
% Simple expression
\addplot table[
x=time,
y expr=\thisrow{value}/1000 % Convert to thousands
] {data.csv};
% Complex expression
\addplot table[
x expr=\thisrow{time}/60, % Convert to minutes
y expr=log10(\thisrow{value}) % Log scale
] {data.csv};
% Using multiple columns
\addplot table[
x=time,
y expr=\thisrow{value1} + \thisrow{value2}
] {data.csv};
\end{axis}Filtering Data
\begin{axis}
% Filter rows by condition
\addplot table[
x=time,
y=value,
restrict y to domain=0:100, % Only y values 0-100
restrict x to domain=0:10 % Only x values 0-10
] {data.csv};
% Unbounded filter
\addplot table[
x=time,
y=value,
unbounded coords=jump % or: discard
] {data.csv};
\end{axis}Multiple Data Series
% data.csv with multiple y columns
% x,y1,y2,y3
% 0,1,2,3
% 1,2,4,6
\begin{axis}[legend pos=north west]
\addplot table[x=x, y=y1, col sep=comma] {data.csv};
\addplot table[x=x, y=y2, col sep=comma] {data.csv};
\addplot table[x=x, y=y3, col sep=comma] {data.csv};
\legend{Series 1, Series 2, Series 3}
\end{axis}3D Data from File
% data3d.dat (matrix format for surf plots)
% 0 1 2 3
% 1 2 3 4
% 4 5 6 7
% 9 10 11 12
\begin{axis}[view={60}{30}]
\addplot3[surf] table {data3d.dat};
\end{axis}
% Or with explicit x,y,z columns
\begin{axis}
\addplot3[only marks] table[x=x, y=y, z=z] {points.csv};
\end{axis}Next Steps
Continue learning PGFPlots:
- Error Bars - Visualize data uncertainty
- Scatter Plots - Plot file data as points
- Line Plots - Connect data points
Data from Files in PGFPlots
Import and visualize data from external files like CSV, TSV, and plain text.
Basic File Import
Given a file data.txt with space-separated values:
# data.txt
x y
0 0
1 1
2 4
3 9
4 16\begin{axis}
\addplot table {data.txt};
\end{axis}CSV Files
# data.csv
time,value,error
0,1.2,0.1
1,2.5,0.2
2,3.8,0.15
3,5.1,0.25\begin{axis}[
xlabel={Time},
ylabel={Value}
]
\addplot table[
col sep=comma, % Comma-separated
x=time, % Column name for x
y=value % Column name for y
] {data.csv};
\end{axis}Column Selection
\begin{axis}
% By column name
\addplot table[x=time, y=value] {data.csv};
% By column index (0-based)
\addplot table[x index=0, y index=1] {data.txt};
% Using expressions
\addplot table[
x=time,
y expr=\thisrow{value}*2 % Double the y values
] {data.csv};
\end{axis}Error Bars from File
\begin{axis}
\addplot[
mark=*,
error bars/.cd,
y dir=both,
y explicit
] table[
x=time,
y=value,
y error=error, % Error column
col sep=comma
] {data.csv};
\end{axis}Tab-Separated Files
\begin{axis}
\addplot table[
col sep=tab % Tab-separated
] {data.tsv};
\end{axis}Skipping Header Rows
\begin{axis}
\addplot table[
skip first n=2, % Skip first 2 rows
% or
ignore chars={\#}, % Ignore lines starting with #
comment chars={\%} % Treat % as comment
] {data.txt};
\end{axis}Inline Data
\begin{axis}
\addplot table {
x y
0 0
1 1
2 4
3 9
4 16
};
\end{axis}
% Or using filecontents
\begin{filecontents}{mydata.dat}
x y
0 1
1 2
2 4
\end{filecontents}
\begin{axis}
\addplot table {mydata.dat};
\end{axis}Mathematical Operations on Data
\begin{axis}
% Simple expression
\addplot table[
x=time,
y expr=\thisrow{value}/1000 % Convert to thousands
] {data.csv};
% Complex expression
\addplot table[
x expr=\thisrow{time}/60, % Convert to minutes
y expr=log10(\thisrow{value}) % Log scale
] {data.csv};
% Using multiple columns
\addplot table[
x=time,
y expr=\thisrow{value1} + \thisrow{value2}
] {data.csv};
\end{axis}Filtering Data
\begin{axis}
% Filter rows by condition
\addplot table[
x=time,
y=value,
restrict y to domain=0:100, % Only y values 0-100
restrict x to domain=0:10 % Only x values 0-10
] {data.csv};
% Unbounded filter
\addplot table[
x=time,
y=value,
unbounded coords=jump % or: discard
] {data.csv};
\end{axis}Multiple Data Series
% data.csv with multiple y columns
% x,y1,y2,y3
% 0,1,2,3
% 1,2,4,6
\begin{axis}[legend pos=north west]
\addplot table[x=x, y=y1, col sep=comma] {data.csv};
\addplot table[x=x, y=y2, col sep=comma] {data.csv};
\addplot table[x=x, y=y3, col sep=comma] {data.csv};
\legend{Series 1, Series 2, Series 3}
\end{axis}3D Data from File
% data3d.dat (matrix format for surf plots)
% 0 1 2 3
% 1 2 3 4
% 4 5 6 7
% 9 10 11 12
\begin{axis}[view={60}{30}]
\addplot3[surf] table {data3d.dat};
\end{axis}
% Or with explicit x,y,z columns
\begin{axis}
\addplot3[only marks] table[x=x, y=y, z=z] {points.csv};
\end{axis}Next Steps
Continue learning PGFPlots:
- Error Bars - Visualize data uncertainty
- Scatter Plots - Plot file data as points
- Line Plots - Connect data points