1

My example as below:

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

\begin{filecontents*}{data.csv}
(10-18-2018 16:50:27.871,0)
(10-18-2018 16:50:27.888,8)
(10-18-2018 16:50:27.904,25)
(10-18-2018 16:50:27.921,41)
(10-18-2018 16:50:27.938,59)
(10-18-2018 16:50:27.954,75)
(10-18-2018 16:50:27.971,92)
(10-18-2018 16:50:27.988,45)
(10-18-2018 16:50:28.005,62)
(10-18-2018 16:50:28.022,79)
(10-18-2018 16:50:28.039,96)
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xticklabel style= {rotate=60,anchor=north east},
xlabel=X,
ylabel=Y
]
\addplot table[ignore chars={(,)},col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Obviously:

  1. x label not correct, I wish it can be automatical match data range. I don't wish to add a min-x/max-x statement.
  2. data point not distributed to the whole graph but all in the center.sounds like microsecons not being parsed!
3
  • I haven't used pgfplots in a looong time, but it looks like the date & timestamp isn't parsed properly at all. – henry Oct 18 '18 at 17:42
  • 1
    pgfplots doesn't handle any smaller interval than minutes for date input. Even seconds will be ignored (set to zero), so you can't expect microseconds to work. I'd suggest calculating the microseconds elapsed from the first data point, and stating in xlabel or figure caption "Microseconds from dd-mm-yyyy HH:MM:SS.SSS" or something. – Torbjørn T. Oct 18 '18 at 19:53
  • See also tex.stackexchange.com/questions/118676/… – Torbjørn T. Oct 18 '18 at 19:55
3

Ulrike Fischer has introduced in this very nice answer a universal parser. (Of course, if you want to vary both the year and the milliseconds, you'll run into dimension too large errors but with Ulrike's nice parser you can just focus on the data you care about.) (I also stress that she used the terminology \marmottimeparseaux, so this is one of the cases where I am not to blame for increasing the marmot awareness. ;-)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
%\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.16}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
(10-18-2018 16:50:27.871,0)
(10-18-2018 16:50:27.888,8)
(10-18-2018 16:50:27.904,25)
(10-18-2018 16:50:27.921,41)
(10-18-2018 16:50:27.938,59)
(10-18-2018 16:50:27.954,75)
(10-18-2018 16:50:27.971,92)
(10-18-2018 16:50:27.988,45)
(10-18-2018 16:50:28.005,62)
(10-18-2018 16:50:28.022,79)
(10-18-2018 16:50:28.039,96)
\end{filecontents*}

\usepackage{xfp}
% from https://tex.stackexchange.com/a/445564/121799
\def\marmottimeparseaux#1-#2-#3 #4:#5:#6xxx{\fpeval{#5* 60 + #6}}
\ExplSyntaxOn
\cs_new:Nn \marmot_timeparse:n { \marmottimeparseaux #1xxx }
\newcommand\TimeParse[1]{\exp_args:Nf\marmot_timeparse:n {#1}}
\ExplSyntaxOff

\begin{document}
 \begin{tikzpicture}
  \begin{axis}
   \addplot table [,ignore chars={(,)},col sep=comma,
   x expr=\TimeParse{\thisrowno{0}},y expr=\thisrowno{1}] {data.csv};
  \end{axis}
 \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.