Graphics, Figures & TablesPlotting averaged data from multiple files

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Plotting averaged data from multiple files

Post by jhapk »

I have N data files (say N=2). They all have same number of data points in the following format (the values of r's is the same in all files. Other values can be anything):

file1.dat:

Code: Select all

r ux
1 0.21
2 0.34
.
.
10 0.12
file2.dat:

Code: Select all

r uy
1 0.51
2 0.34
.
.
10 0.23
I want to plot (ux+uy)/2 versus r on a log-log plot. (How) Can I do that without generating a new data file?

The code I use for plotting ux and uy separately on the same graph is something like this.

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}

\begin{loglogaxis}[
    x=r
  ]
  \addplot[
    only marks,
    mark=*
  ] table [
    y=ux
  ]{file1.dat};
  \addplot[
    only marks,
    mark=*
  ] table [
    y=uy
  ]{cl_statistics.dat};
\end{loglogaxis}
\end{tikzpicture}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Plotting averaged data from multiple files

Post by hugovdberg »

Try the pgfplotstable-package. This can create tables in memory from one or more existing files. I'm currently not near a computer to provide an example, but take a look at section 4 of the manual, the \pgfplotstablenew command and create col/copy column from table option to \pgfplotstableset can be used to create a table accessible with a macro that can be plotted using \addplot table.

Hope this gets you started, if you need more help I can probably delve in it more deeply tonight (CET).
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Plotting averaged data from multiple files

Post by hugovdberg »

As a follow up on this mornings' post, I created a small example collecting data from two files, and plotting the average values of the two columns y1 and y2, from file1.dat and file2.dat, respectively.

Code: Select all

%% Create the two datafiles to make a selfcontained example
\begin{filecontents}{file1.dat}
x	y1
0	10
1	11
2	12
3	13
4	14
5	15
6	16
7	17
8	18
9	19
10	20
\end{filecontents}

\begin{filecontents}{file2.dat}
x	y2
0	10
1	12
2	6
3	8
4	4
5	6
6	3
7	5
8	2.5
9	4.5
10	2.25
\end{filecontents}

\documentclass{article}

\usepackage{tikz, pgfplots, pgfplotstable}
\pgfplotsset{compat=1.8}

\begin{document}

% Read the contents of file1.dat into the macro \datafile
\pgfplotstableread{file1.dat}\datafile

% Add a new column in the macro \datafile with the name y2, with data read from column y2 in file2.dat
% Note that this does not check whether the x values in the macro and file2.dat correspond
\pgfplotstablecreatecol[
	create col/copy column from table={file2.dat}{y2}
	]{y2}{\datafile}

% Typeset the table \datafile from memory
\begin{table}
\centering
\pgfplotstabletypeset{\datafile}
\end{table}

% Plot the average value of columns y1 and y2 against the values in column x
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
\addplot table [x=x, y expr={(\thisrow{y1} + \thisrow{y2})/2}] {\datafile};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Plotting averaged data from multiple files

Post by jhapk »

Thanks!! that worked perfectly. I have a follow up question.

I have data files with multiple columns. I want to add N number of columns of another data file to the macro "datafile" (in your example) created from the first data file. Is there a better way of doing it than as I am doing right now (adding each column one after another)?

Code: Select all

****header code ****

\pgfplotstableread{file1.dat}\datafile                                                                          
                                                                                                                        
\pgfplotstablecreatecol[                                                                                                
        create col/copy column from table={file2.dat}{v2}                                                      
        ]{v2}{\datafile}                                                                                               
\pgfplotstablecreatecol[                                                                                                
        create col/copy column from table={file2.dat}{v3}                                                      
        ]{v3}{\datafile}                                                                                               
\pgfplotstablecreatecol[                                                                                                
        create col/copy column from table={file3.dat}{w2}                                                      
        ]{w2}{\datafile}                                                                                               
\pgfplotstablecreatecol[                                                                                                
        create col/copy column from table={file3.dat}{w3}                                                      
        ]{w3}{\datafile}

****code ****
hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Plotting averaged data from multiple files

Post by hugovdberg »

Whether there is a faster way depends on your file names and column names. You could at least define a macro that saves typing of options that are the same for each column. If your columnnames are the same in each file you could use a \foreach-loop, from package pgffor, to loop over a list of files, and the list of columns in each file. If the columnnames in all files are unique you could write a loop for each file.

To clarify, first an example with identical column names in all files:

Code: Select all

%% Copy column takes 4 arguments: input table, input column, output table, output column
\newcommand{\copycolumn}[4]{
\pgfplotstablecreatecol[                                                                                                
        create col/copy column from table={#1}{#2}                                                      
        ]{#4}{#3}   
}

\pgfplotstableread{file1.dat}\datafile                                                                          

% provide filenames without ".dat"
\foreach \file in {file2, file3, file4}{
   \foreach \column in {column1, column2, column3}{
      \copycolumn{\file.dat}{\column}{\datafile}{\file\column} % make sure column names are unique in output
   }
};
If the columnnames are unique across all files, or the number of columns in each file is not the same, you could do something like this:

Code: Select all

%% Copy column takes 3 arguments: column, input table, output table
\newcommand{\copycolumn}[3]{
\pgfplotstablecreatecol[                                                                                                
        create col/copy column from table={#2}{#1}                                                      
        ]{#1}{#3}   
}

\pgfplotstableread{file1.dat}\datafile                                                                          

\foreach \column in {column1, column2, column3}{
   \copycolumn{\column}{file2.dat}{\datafile}
};
\foreach \column in {column4, column5, column6, column7}{
   \copycolumn{\column}{file3.dat}{\datafile}
};
\foreach \column in {column8, column9}{
   \copycolumn{\column}{file4.dat}{\datafile}
};
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
Post Reply