2

I have data in a .csv file with seconds as x values. I'd like to tick the x axis in hours:minute formate e.g.: 0:00 , 0:30 , 1:00 , 1:30 ...

How do I have to redefine the interpretation of values to fit into that different presentation style?

data sample

    seconds values
    30       3.98
    60       3.97
    90       3.96
    120      3.96
    150      3.95
    ...      ...
   3210      3.45
7
4

You can convert the seconds to hours by using an x filter, and then split the hours and minuts for the labels within the xticklabel expression:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    % Convert seconds to hours
    x filter/.code={\pgfmathparse{#1/3600}},
    xticklabel={ % Split into hours and minutes
        \pgfmathsetmacro\hours{floor(\tick)}%
        \pgfmathsetmacro\minutes{(\tick-\hours)*0.6}%
        % Use some trickery to get leading zeros
        \pgfmathprintnumber{\hours}:\pgfmathprintnumber[fixed, fixed zerofill, skip 0.=true, dec sep={}]{\minutes}%
    },
    xtick={0,0.25,...,1}
]
\addplot table {
    seconds values
    30       3.98
    60       3.97
    90       3.96
    120      3.96
   3210      3.95
};
\end{axis}
\end{tikzpicture}
\end{document}

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.