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
every nth point={<integer>}
option of pgfplots to reduce the input data, e.g. likeevery nth point=500
..tex
file?