使用 PGFPLOT 在两个轴上显示日期/时间

使用 PGFPLOT 在两个轴上显示日期/时间

我正在尝试绘制一些在 x 轴和 y 轴上都包含日期/时间值的数据。

我一直在使用 pgfplots,并设法使用 \usepgfplotslibrary{dateplot} 和自定义 \plotset{.....} 分别在 x 轴和 y 轴上绘制日期/时间(见这个问题这个问题

但是,我无法将日期/时间合并到同一张图表的两个轴上。

您能建议如何解决这个问题吗?

我的尝试数据如下:

TIMESTAMP, TIMEDELAY
2015-08-02 06:00, 00:03
2015-08-02 06:30, 00:02
2015-08-02 07:00, 00:03
2015-08-02 07:30, 00:03
2015-08-02 08:00, 00:03
2015-08-02 08:30, 00:03
2015-08-02 09:00, 00:04
2015-08-02 09:30, 00:02
2015-08-02 10:00, 00:03
2015-08-02 10:30, 00:03
2015-08-02 11:00, 00:03
2015-08-02 11:30, 00:03
2015-08-02 12:00, 00:04
2015-08-02 12:30, 00:03
2015-08-02 13:00, 00:03
2015-08-02 13:30, 00:03
2015-08-02 14:00, 00:04
2015-08-02 14:30, 00:04
2015-08-02 15:00, 
2015-08-02 15:30, 00:03
2015-08-02 16:00, 00:07
2015-08-02 16:30, 00:02
2015-08-02 17:00, 00:03
2015-08-02 17:30, 00:03
2015-08-02 18:00, 00:03

包含空白数据点(例如 15:00)

我成功尝试在 Y 轴上绘制时间戳

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{dateplot}

\def\transformtime#1:#2!{
    \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
    \pgfmathparse{#1*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+#2*60}
    \pgfkeys{/pgf/fpu=false}
}

\pgfplotsset{
    timeplot zero/.initial=0,
    timeplot/.style={
        y coord trafo/.code={\expandafter\transformtime##1!},
        y coord inv trafo/.code={%
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed,}
            \pgfmathsetmacro\hours{floor(##1/3600)+\pgfkeysvalueof{/pgfplots/timeplot zero}}
            \pgfmathsetmacro\minutes{floor((##1-(\hours - \pgfkeysvalueof{/pgfplots/timeplot zero})*3600)/60)}
            \pgfmathsetmacro\seconds{##1-floor((##1)/60)*60}
            \def\pgfmathresult{\pgfmathprintnumber{\hours}:\pgfmathprintnumber{\minutes}:\pgfmathprintnumber[fixed zerofill]{\seconds}}
            \pgfkeys{/pgf/fpu=false}
        },
    scaled y ticks=false,
    yticklabel=\tick
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    timeplot, timeplot zero=0, grid=major, grid style=dashed, ,
]
\addplot table {
State           Time  
6 00:03
6.5 00:02
7 00:03
7.5 00:03
8 00:03
8.5 00:03
9 00:04
9.5 00:02
10 00:03
10.5 00:03
11 00:03
11.5 00:03
12 00:04
12.5 00:03
13 00:03
13.5 00:03
14 00:04
14.5 00:04
15 00:03
};
\end{axis}
\end{tikzpicture}
\end{document}

我成功地在 X 轴上绘制了时间。(虽然在使用 dateplot 时我不得不使用 csv 文件

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pagestyle{empty}



\begin{document}

\begin{tikzpicture}
\begin{axis}[date coordinates in=x,]
\addplot table[col sep=comma] {datedata2.csv};;
\end{axis}
\end{tikzpicture}

\end{document}

答案1

像这样吗?

在此处输入图片描述

空白数据点仍然是一个问题。

\documentclass[border=10pt]{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}

\def\transformtime#1:#2!{
    \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
    \pgfmathparse{#1*3600-\pgfkeysvalueof{/pgfplots/timeplot zero}*3600+#2*60}
    \pgfkeys{/pgf/fpu=false}
}

\pgfplotsset{
    timeplot zero/.initial=0,
    timeplot/.style={
        y coord trafo/.code={\expandafter\transformtime##1!},
        y coord inv trafo/.code={%
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed,}
            \pgfmathsetmacro\hours{floor(##1/3600)+\pgfkeysvalueof{/pgfplots/timeplot zero}}
            \pgfmathsetmacro\minutes{floor((##1-(\hours - \pgfkeysvalueof{/pgfplots/timeplot zero})*3600)/60)}
            \def\pgfmathresult{
                \pgfmathprintnumber{\hours}:%
                \pgfmathparse{int(mod(\minutes,60))/100}%
                    \pgfmathprintnumber[skip 0.=true, dec sep={}, fixed]{\pgfmathresult}
                }
            \pgfkeys{/pgf/fpu=false}
        },
        scaled y ticks=false,
        yticklabel=\tick
    }
}

\usepackage{filecontents}
\begin{filecontents}{datedata2.csv}
TIMESTAMP, TIMEDELAY
2015-08-02 06:00, 00:03
2015-08-02 06:30, 00:02
2015-08-02 07:00, 00:03
2015-08-02 07:30, 00:03
2015-08-02 08:00, 00:03
2015-08-02 08:30, 00:03
2015-08-02 09:00, 00:04
2015-08-02 09:30, 00:02
2015-08-02 10:00, 00:03
2015-08-02 10:30, 00:03
2015-08-02 11:00, 00:03
2015-08-02 11:30, 00:03
2015-08-02 12:00, 00:04
2015-08-02 12:30, 00:03
2015-08-02 13:00, 00:03
2015-08-02 13:30, 00:03
2015-08-02 14:00, 00:04
2015-08-02 14:30, 00:04
%2015-08-02 15:00, nan
2015-08-02 15:30, 00:03
2015-08-02 16:00, 00:07
2015-08-02 16:30, 00:02
2015-08-02 17:00, 00:03
2015-08-02 17:30, 00:03
2015-08-02 18:00, 00:03
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    timeplot, timeplot zero=0, grid=major, grid style=dashed,
    date coordinates in=x,
    x tick label style={align=center, rotate=45, font=\scriptsize},
    ]
    \addplot table[col sep=comma] {datedata2.csv};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容