[Tex/LaTex] Pgfplots: Assemble dates from different columns

pgfplots

I am trying to plot a datafile of a time series in pgfplots. The date is not in the YYYY-MM-DD hh:mm format required by the PGF calendar library (see p. 210 in the pgfplots manual), but in different columns called year, month, day etc.

Is there a way of joining different columns into a new string in the required format on the fly when calling \addplot table (maybe with something like x expr=...)? I'm trying to avoid using \pgfplotstableread with a create on use command because I have found that to take surprisingly long to run.

Here's an example of the datafile format I'm talking about:

year, month, day, hour, minute, value  
2006, 5, 21, 1, 0, 10.5  
2006, 5, 22, 2, 0, 23.4  
2006, 5, 22, 4, 5, 30.1  
2006, 5, 27, 3, 0, 35.2

Best Answer

I am aware that this question is quite old now... but in case there is still interest in an answer: here is one which works "on-the-fly":


\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=newest}
\begin{document}

\pgfplotsset{
    custom date format/.code={
        % set the normal data format:
        \pgfkeysalso{date coordinates in=x}%
        % query the 'x coord trafo' key's value:
        \pgfkeysgetvalue{/pgfplots/x coord trafo/.@cmd}\temp
        % store it (globally):
        \global\let\theolddatacoordtrafo=\temp
        % redefine it:
        \pgfkeysdef{/pgfplots/x coord trafo}{%
            % evaluate the expression:
            \edef\temp{#1}%
            \message{Coordinate \coordindex: Using \temp.^^J}%
            % now, insert the *value* of '\temp' as argument to
            % the original 'x coord trafo':
            \expandafter\theolddatacoordtrafo\temp\pgfeov
        }%
    },
}
\begin{tikzpicture}
\begin{axis}[
    custom date format={\thisrow{year}-\thisrow{month}-\thisrow{day} \thisrow{hour}:\thisrow{minute}}, 
    % pretty-printing:
    width=\linewidth,
    xticklabel style={rotate=90,anchor=near xticklabel},
    xticklabel=\day. \hour:\minute,
]
\addplot table[col sep=comma,y=value] {

year, month, day, hour, minute, value 2006, 5, 21, 1, 0, 10.5 2006, 5, 22, 2, 0, 23.4 2006, 5, 22, 4, 5, 30.1 2006, 5, 27, 3, 0, 35.2 }; \end{axis} \end{tikzpicture} \end{document}

My idea here was to define a custom 'x coord trafo', which concatenates your columns in a "proper" way and then hands the concatenated and expanded ('\edef' = expanded definition) result to the 'x coord trafo' which is used by the dateplot lib.

Consequently, the dataplot lib is unaware of the preprocessing step and the preprocessing step is performed on-the-fly.

Actually, the feature to assemble symbolic coordinates from tables is missing in pgfplots. I have taken a note on my todo list such that future versions will (eventually) support something like the 'x symbolic expr={\thisrow{year}-\thisrow{month}...}'.

Related Question