带有日期 x 轴的图表:如何显示非军事时间?

带有日期 x 轴的图表:如何显示非军事时间?

示例代码:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usepgfplotslibrary{dateplot}

\begin{document}
% requires \usepgfplotslibrary{dateplot} !
\begin{tikzpicture}
  \begin{axis}[
    date coordinates in=x,
    xtick=data,
    xticklabel style=
    {rotate=90,anchor=near xticklabel},
    xticklabel=\day. \hour:\minute,
    date ZERO=2009-08-18
  ]
  \addplot coordinates {
    (2009-08-18 09:00,  050)
    (2009-08-18 12:00,  100)
    (2009-08-18 15:00,  100)
    (2009-08-18 18:35,  100)
    (2009-08-18 21:30,  040)
    (2009-08-19,        020)
    (2009-08-19 3:00,   000)
    (2009-08-19 6:0,    035)
  };
  \end{axis}
\end{tikzpicture}

\end{document}

我想将其更改xticklabel为不显示军事时间;我想要 PM/AM 符号。这可能吗?

在此处输入图片描述

答案1

这是一个可能的解决方法;我使用前导零来对齐日期。

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usepgfplotslibrary{dateplot}

\makeatletter
\newcommand{\nonmiltime}{%
  \ifnum\hour>12
    \expandafter\two@digits\expandafter{\the\numexpr\hour-12\relax}%
  \else
    \expandafter\two@digits\expandafter{\hour}%
  \fi
  :\minute\,%
  \ifnum\hour>11
    \textsc{pm}%
  \else
    \textsc{am}%
  \fi
}
\makeatother

\begin{document}
% requires \usepgfplotslibrary{dateplot} !
\begin{tikzpicture}
  \begin{axis}[
    date coordinates in=x,
    xtick=data,
    xticklabel style=
        {rotate=90,anchor=near xticklabel},
        xticklabel=\day. \nonmiltime,
        date ZERO=2009-08-18
  ]
  \addplot coordinates {
    (2009-08-18 09:00,  050)
    (2009-08-18 12:00,  100)
    (2009-08-18 15:00,  100)
    (2009-08-18 18:35,  100)
    (2009-08-18 21:30,  040)
    (2009-08-19,        020)
    (2009-08-19 3:00,   000)
    (2009-08-19 6:0,    035)
  };
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容