# datetime plot in pgfplot using gnuplot

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

• I think using more than 2400 data points (8 inches wide paper * 300 dots/per inch = 2400 dots). So you could use the every nth point={<integer>} option of pgfplots to reduce the input data, e.g. like every nth point=500. – Tom Bombadil Oct 26 '13 at 22:54
• How to you call the gnuplot script from within pgfplots? Could you show your .tex file? – Christian Feuersänger Oct 27 '13 at 18:06

## 1 Answer

gnuplot does not seem to generate any valid x coordinate (only useless strings), and pgfplots chokes on that. I experimented a bid but failed to reconfigure gnuplot accordingly.

If you cannot get gnuplot to write suitable x coordinates, I suppose the solution is to invoke gnuplot manually (or by writing \immediate\write18{<system call>}) and then using pgfplots 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 run gnuplot, let it generate some numerical x coordinate, read that into pgfplots 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:

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{dateplot}

\begin{document}
\begin{tikzpicture}
\begin{axis} [width=21cm, height=14cm,
date ZERO=2012-06-01,
date coordinates in=x,
max space between ticks=70pt,
xticklabel=\month-\day\space\Hour:\Minute,
]

\addplot table[col sep=comma] {datafile.dat};

\end{axis}
\end{tikzpicture}
\end{document}