1

Here's the code I want to compile:

\documentclass{article}

\usepackage{tikz, pgfplots} \usepgfplotslibrary{dateplot}

% from https://tex.stackexchange.com/a/288225/231104

\def\pgfplotslibdateplothour:minutetofloat#1:#2.{ \pgfmathparse{#1+#2/60} } \def\pgfplotslibdateplofloattothour:minute#1.{ \pgfmathsetmacro\hour{int(floor(#1))} \pgfmathsetmacro\minute{int((#1-\hour)*60)} \ifnum\hour<10\edef\hour{0\hour}\fi \ifnum\minute<10\edef\minute{0\minute}\fi } \pgfplotsset{ /pgfplots/time coordinates in/.code={% \pgfkeysdef{/pgfplots/#1 coord trafo}{% \pgfplotslibdateplothour:minutetofloat##1. } \pgfkeysdef{/pgfplots/#1 coord inv trafo}{ \pgfplotslibdateplofloattothour:minute##1. } } }

\begin{document}

\begin{tikzpicture} \begin{axis}[ time coordinates in=x, xticklabel={\hour:\minute}, ] \addplot coordinates { (10:00, 0) (10:30, 25) (10:40, 28) (11:00, 40) (11:15, 42) (11:30, 50) }; \end{axis} \end{tikzpicture}

\end{document}

But on compiling, it just gives a 'Something went wrong' sign. Seems like the code misses something essential, but I couldn't find what.

Please help, thanks!

1 Answers1

1

The PGFPLOTS manual mentions in the chapter on user transformations (page 386 for version 1.17):

Remark: It might be necessary to set \pgfplotsset{ xticklabel={\tick}, scaled x ticks=false, plot coordinates/math parser=false, } in order to avoid number formatting routines on \tick or numerics for tick scale methods.

A bit of experimentation shows that only plot coordinates/math parser=false is necessary in this case. This allows the function defined in the code to transform for example 10:30 to 10.5, which PGFplots can use to find the correct position on the x axis.

Another issue is the inverse transformation, from for example 10.5 back to 10:30. Because there is a . in 10.5 the pattern in the float to hour function, which is floattothour:minute#1., sees only the 10 and then assumes the argument is complete. Changing this to, e.g., : fixes this issue, so floattothour:minute#1:, in the function header but also in the call within coord inv trafo.

MWE:

\documentclass{article}

\usepackage{tikz, pgfplots} \pgfplotsset{compat=newest} \usepgfplotslibrary{dateplot}

% from https://tex.stackexchange.com/a/288225/231104

\def\pgfplotslibdateplothour:minutetofloat#1:#2.{ \pgfmathparse{#1+#2/60} } \def\pgfplotslibdateplofloattothour:minute#1:{ \pgfmathsetmacro\hour{int(floor(#1))} \pgfmathsetmacro\minute{int((#1-\hour)*60)} \ifnum\hour<10\edef\hour{0\hour}\fi \ifnum\minute<10\edef\minute{0\minute}\fi } \pgfplotsset{ /pgfplots/time coordinates in/.code={% \pgfkeysdef{/pgfplots/#1 coord trafo}{% \pgfplotslibdateplothour:minutetofloat##1. } \pgfkeysdef{/pgfplots/#1 coord inv trafo}{ \pgfplotslibdateplofloattothour:minute##1: } } }

\begin{document}

\begin{tikzpicture} \begin{axis}[ plot coordinates/math parser=false, time coordinates in=x, xticklabel={\hour:\minute}, ] \addplot coordinates { (10:00, 0) (10:30, 25) (10:40, 28) (11:00, 40) (11:15, 42) (11:30, 50) }; \end{axis} \end{tikzpicture}

\end{document}

Result:

enter image description here

Marijn
  • 37,699