将日期图的分辨率提高到秒

将日期图的分辨率提高到秒

这三个点应该分散在50秒的时间内,但不知为何它们都出现在13:00:30。

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

\begin{document}
\begin{tikzpicture}

  \begin{axis}[
    date coordinates in=x,
    xtick=data,
    xticklabel={\hour:\minute:\second},
    scaled x ticks=false, 
    xticklabel style={anchor=east,rotate=45},
    xticklabels={13:00:30,
      13:00:40,
      13:00:50}
    ]
    \addplot table [col sep=comma,trim cells=true,y=value1,scatter,only marks] {data.csv};
  \end{axis}

\end{tikzpicture}
\end{document}

data.csv看起来像这样:

date,                   value1
2013-06-03 13:00:00,       2
2013-06-03 13:00:15,       3
2013-06-03 13:00:50,       14

如果您将第 3 行更改为 eg,13:01:00它就会非常有效。那么我该如何提高pgfplot“分辨率”呢?

答案1

由于精度有限,dateplot附带的库无法处理秒数。pgfplots

如果您需要这种精度,您可能可以忽略输入的 DATE 部分。在这种情况下,解决方案可能如下:

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

\def\checkSameDate#1{%
    \ifnum\coordindex=0
        \gdef\itsDate{#1}%
    \else
        \def\temp{#1}%
        \ifx\temp\itsDate
        \else
            \PackageError{custom}{Sorry, expected the same date but got two different ones \itsDate\space and #1}{}%
        \fi
    \fi
}%

\def\ensuretwodigits#1{%
    \ifnum#1<10
        \edef\pgfmathresult{0#1}%
    \else
        \edef\pgfmathresult{#1}%
    \fi
}%

\pgfplotsset{
    % invoked with
    % 2013-06-03 23:59:00
    % -> 
    %  #1 = 2013
    %  #2 = 06
    %  #3 = 03
    %  #4 = 23
    %  #5 = 59
    %  #6 = 00
    @datetime to number/.code args={#1-#2-#3 #4:#5:#6}{%
        \checkSameDate{#1-#2-#3}%
        % 
        % convert to full seconds:
        \pgfmathparse{#4*3600+60*#5 + #6}%
    },
    %
    % This is a style which activates the new feature:
    date same day in x/.style={%
        x coord trafo/.style={/pgfplots/@datetime to number=##1},
        scaled x ticks=false,
        plot coordinates/math parser=false,
        %
        % the following keys are ONLY here to format tick labels
        x coord inv trafo/.code={%
            \begingroup
            % compute the value
            \pgfkeys{/pgf/fpu}%
            %
            % ... \seconds
            \pgfmathparse{mod(##1,60)}%
            \pgfmathfloattoint{\pgfmathresult}%
            \ensuretwodigits{\pgfmathresult}%
            \global\let\seconds=\pgfmathresult
            %
            % ... \minutes
            \pgfmathparse{mod( (##1 - \seconds)/60,60)}%
            \pgfmathfloattoint{\pgfmathresult}%
            \ensuretwodigits{\pgfmathresult}%
            \global\let\minutes=\pgfmathresult
            %
            % ... \hours
            \pgfmathparse{(##1 - 60*\minutes - \seconds)/3600}%
            \pgfmathfloattoint{\pgfmathresult}%
            \ensuretwodigits{\pgfmathresult}%
            \global\let\hours=\pgfmathresult
            \endgroup
        },
        xticklabel style={
            /pgf/number format/fixed,
        },
        xticklabel=\hours:\minutes:\seconds,
    },
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    %xtick=data,
    xticklabel style={anchor=east,rotate=45},
    date same day in x,
    ]
    \addplot table [col sep=comma,trim cells=true,y=value1,scatter,only marks] {
date,                   value1
2013-06-03 23:59:00,       2
2013-06-03 23:59:10,       3
2013-06-03 23:59:20,       3
2013-06-03 23:59:30,       3
2013-06-03 23:59:40,      3
2013-06-03 23:59:50,       14
2013-06-03 23:59:59,       14
};
  \end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

我的想法是提供一个简单的转换,忽略日期部分并将时间部分映射到数字。我添加了一个健全性检查,以验证日期部分是否相同。

编辑:我添加了一个逆变换,它允许自动格式化刻度标签,即使pgfplots自动选择刻度位置。

相关内容