Feb 27, 2010

Plot data file with PGFPlot

Tikz and pgfplot can plot data file in Tex.

\begin{figure}
\begin{center}
\begin{tikzpicture}[scale=0.3]
\begin{axis}[xlabel=nstep, ylabel=WaveHeight]
\addplot[mark=none] file {case14.data};   % Here is the data file
\addplot[color=green,mark=none] file {case14xb.data};
\legend{True, Predicted};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale=0.3]
\begin{axis}[xlabel=nstep, ylabel=error(2-norm)]
\addplot[mark=none] file {case14err.data};
\end{axis}
\end{tikzpicture}
\end{center}
\caption{(a)True and predicted wave height at the domain center; (b)2-norm of numerical error on the whole domain}
\end{figure}


The data file's format can be multiple columns separated by space. Each column is for one variable. Such as
# data file
0.00001 2.123243
0.00003 3.123452
......
Sometimes, Matlab's fprintf function may help to generate such data file.
For example:
a=load('err14.dat');
fid = fopen('case14err.data','w');
b=1/650:6.5/650:6.5;
y=[b ; a];
fprintf(fid,'%1.5f %1.5f\n', y);
fclose(fid);

2 comments:

Mark Lim said...

Great site.
Thanks

Mark Lim said...

Great site.
Thanks