使用带有奇数刻度标签的 pgfplots 绘制日期图

使用带有奇数刻度标签的 pgfplots 绘制日期图

如何才能让下图中的时间标签显示为“10:00”(即10点),而不是出乎意料、不受欢迎的“09:60”呢?

在此处输入图片描述

这是我正在使用的代码:

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


\begin{document} 

\frame{
\begin{tikzpicture}
\begin{axis}[
mark =none,
xmin=2009-08-18 08:30,
xmax=2009-08-18 10:29,
ymin=0, 
ymax=189,
grid=both,
axis x line=bottom,
axis y line=left,
date coordinates in=x,
minor x tick num=5,
minor y tick num=4,
xtick={2009-08-18 09:00,2009-08-18 10:00},
ytick={0,50,100,150},
xticklabel= \hour:\minute,
]
\addplot [thick,blue]coordinates {
(2009-08-18 08:30, 000)
(2009-08-18 09:00, 060)
(2009-08-18 09:20, 060)
(2009-08-18 10:00, 100)
(2009-08-18 10:10, 060)
} node [below] {Train A};

\end{axis}
\end{tikzpicture}
}

\end{document}

补充问题:

在上图中,为什么次要刻度线不出现在“09:00”之前,而只出现在之后?

答案1

这是一个已知错误,已在 PGFplots 开发版本中修复。您可以在前言中定义已修补的函数,从而在不升级软件包的情况下包含错误修复。

我不确定网格线出了什么问题,您可能需要提交错误报告。您可以使用以下设置解决此问题:

minor x tick num=11,
xtick={2009-08-18 08:00,2009-08-18 10:00},
extra x ticks=2009-08-18 09:00

完整代码如下:

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

\makeatletter
\def\pgfplotslibdateplot@number@to@julian@and@time#1.#2\julianto#3\hourto#4\minuteto#5{%
    #3=#1
    \pgf@xa=0.#2pt
    \multiply\pgf@xa by24
    \afterassignment\pgfplots@gobble@until@relax
    \c@pgf@countb=\the\pgf@xa\relax
    \edef#4{\the\c@pgf@countb}%
    \advance\pgf@xa by-#4pt
    \multiply\pgf@xa by60
    \afterassignment\pgfplots@gobble@until@relax
    \c@pgf@countb=\the\pgf@xa\relax
    % round minutes (we may lose precision here)
    \advance\pgf@xa by-\the\c@pgf@countb pt
    \ifdim\pgf@xa>0.5pt 
        \advance\c@pgf@countb by1
        \ifnum\c@pgf@countb=60
            \c@pgf@countb=#4 %
            \advance\c@pgf@countb by1
            \edef#4{\the\c@pgf@countb}%
            \c@pgf@countb=0
        \fi
    \fi
    \edef#5{\the\c@pgf@countb}%
}
\makeatother


\begin{document} 

\frame{
\begin{tikzpicture}
\begin{axis}[
mark =none,
date ZERO=2009-08-18 08:30,
xmin=2009-08-18 08:30,
xmax=2009-08-18 10:29,
ymin=0, 
ymax=189,
grid=both,
axis x line=bottom,
axis y line=left,
date coordinates in=x,
minor x tick num=11,
minor y tick num=4,
xtick={2009-08-18 08:00,2009-08-18 10:00},
extra x ticks=2009-08-18 09:00,
ytick={0,50,100,150},
xticklabel= \hour:\minute,
]
\addplot [thick,blue]coordinates {
(2009-08-18 08:30, 000)
(2009-08-18 09:00, 060)
(2009-08-18 09:20, 060)
(2009-08-18 10:00, 100)
(2009-08-18 10:10, 060)
} node [below] {Train A};

\end{axis}
\end{tikzpicture}
}

\end{document}

pgfplots 日期图

相关内容