12

I have a datafile (which I may manipulate to any format), it contains a date-time stamp currently in the form "year-month-day hour:min" and a single variable for each recorded time.

When I attempt to plot this datafile it is parsed as if the date-time stamp is two variables, my question is how should I be formatting the datafile for it to be parsed as a single variable.

Thanks

Phil
  • 675

1 Answers1

18

You should use commas to separate the fields. Then you can plot the file using \addplot table [col sep=comma] {datafile.csv};

If you want to use whitespaces in addition to the commas to keep the file more readable, you need to use [col sep=comma,trim cells=true] to strip the spaces around the commas.

Here's a minimal example of plotting a file with dates and times:

\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
date coordinates in=x,
xticklabel={\day.\month.\year\\ \hour:\minute},
x tick label style={align=center},
date ZERO=2010-01-01, % Set near lowest date
xmin={2010-01-01}, % A date with no time is assumed to have a time of 00:00
xmax={2010-01-04},
xtick={2010-01-01 12:00, % Set tick marks
2010-01-02 12:00,
2010-01-03 12:00}
]
\addplot table [col sep=comma,trim cells=true,y=value1] {data.csv};
\addplot table [col sep=comma,trim cells=true,y=value2] {data.csv};
\end{axis}

\end{tikzpicture}
\end{document}

With a datafile.csv like this:

date,                   value1, value2
2010-01-01 12:00,       2,      0
2010-01-01 23:15,       10,     1
2010-01-03 12:00,       8,      10

would yield the following output:

PGFplots plot of datafile with dates

Jake
  • 232,450