I have a large data file with several columns wherein the first column is a date and time in the format, 'yyyy-mm-dd hh:MM' and the rest are numerical values of different variables. I often have several days of data, sampled at 1 second. This is why I want to use gnuplots instead of plotting it using TeX. The graphics option is another option I could pursue, but I prefer using gnuplots.
I am trying to plot this data in gnuplot within pgfplots
, but am having some trouble. For the sake of simplicity, consider this shortened data file (datafile.dat):
2012-06-01, 01:00, 1
2012-06-01, 02:00, 2
2012-06-01, 03:00, 4
2012-06-01, 04:00, 3
2012-06-02, 01:00, 5
2012-06-02, 02:00, 2
2012-06-02, 03:00, 1
2012-06-02, 04:00, 1
In gnuplot, I am able to plot this data using the following lines:
set xdata time
set timefmt '%Y-%m-%d, %H:%M'
set datafile sep ','
plot 'datafile.dat' u 1:3 w lines
I am now trying to plot the same data in pgfplots using gnuplot but I am not having any success. Is it possible to use the dateplot library in pgfplots with gnuplot?
Hi Christian. Sure. Here is my TeX file:
\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{dateplot}
\makeatletter
{
\catcode`\%=12
\catcode`\"=12
\xdef\pgf@gnuplot@head{set table \noexpand\pgf@plottablefile@quoted; set xdata time; set timefmt "%Y-%m-%d, %H:%M"; set datafile sep ',';}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis} [width=21cm, height=14cm,
date coordinates in=x,
]
\addplot gnuplot[raw gnuplot, id=testMe, mark=none] {
plot "datafile.dat" using ($1):($3) with lines;
};
\end{axis}
\end{tikzpicture}
\end{document}
Here is the generated gnuplot file:
set table "gnu_test.testMe.table"; set xdata time; set timefmt "%Y-%m-%d, %H:%M"; set datafile sep ',';
set format "%.7e";; plot "datafile.dat" using ($1):($3) with lines;
And here is the resulting gnuplot table:
# Curve 0 of 1, 8 points
# Curve title: ""datafile.dat" using ($1):($3)"
# x y type
"" 1.0000000e+000 i
"" 2.0000000e+000 i
"" 4.0000000e+000 i
"" 3.0000000e+000 i
"" 5.0000000e+000 i
"" 2.0000000e+000 i
"" 1.0000000e+000 i
"" 1.0000000e+000 i
Best Answer
gnuplot
does not seem to generate any validx
coordinate (only useless strings), andpgfplots
chokes on that. I experimented a bid but failed to reconfigure gnuplot accordingly.If you cannot get
gnuplot
to write suitablex
coordinates, I suppose the solution is to invoke gnuplot manually (or by writing\immediate\write18{<system call>}
) and then usingpgfplots
to read the resulting table.Note that you also seem to have a minor issue in your time format: the comma in the
timefmt
is useless as it is also a column separator.What I did not quite understand is the purpose of
gnuplot
in this example. Perhaps what you need is to rungnuplot
, let it generate some numerical x coordinate, read that intopgfplots
and assign suitable x tick labels.However,
pgfplots
seems to be doing a reasonable job on your file which I'd like to include here - at the risk of missing a central point of your questions: