具有两个 y 轴和时间格式 x 轴的 PGF 图

具有两个 y 轴和时间格式 x 轴的 PGF 图

我在设置要绘制的图表的坐标轴时遇到了麻烦。我的目标是在 x 轴上以 hh:mm:ss 格式显示时间,并在 y 轴上显示两个不同的时间。不知何故,编译结果很奇怪。此外,我需要从 txt 文件绘制图表,因为我有超过 2700 个测量值。

这是我的代码:

\documentclass[a4paper,12pt,twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[style=iso-numeric]{biblatex}
\addbibresource{references.bib}
\usepackage[slovak]{babel}
\usepackage{graphicx}
\graphicspath{{./images/}}
\usepackage{mathptmx}
\usepackage{amsmath}
\usepackage[labelfont=bf]{caption}
\usepackage{chngcntr}
\counterwithin{figure}{section}
\counterwithin{equation}{section}
\counterwithin{table}{section}
\makeatletter
\providecommand\add@text{}
\newcommand\tagaddtext[1]{%
  \gdef\add@text{#1\gdef\add@text{}}}% 
\renewcommand\tagform@[1]{%
  \maketag@@@{\llap{\add@text\quad}(\ignorespaces#1\unskip\@@italiccorr)}%
}
\makeatother
\usepackage{filecontents}
\usepackage{pgf-pie}
\usepackage{threeparttable}
\usepackage[nottoc]{tocbibind}
\usepackage{xcolor}
\usepackage{pdfpages}
\usepackage{enumerate}
\usepackage{blindtext}
\usepackage{enumitem}
\usepackage{pdflscape}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{subfig}
\usepackage{setspace}
\usepackage{siunitx}
\makeatletter
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\pgfplotslibdateplothour:minutetofloat#1:#2.{
    \pgfmathparse{#1+#2/60}
}
\def\pgfplotslibdateplofloattothour:minute#1.{
    \pgfmathsetmacro\hour{int(floor(#1))}
    \pgfmathsetmacro\minute{int((#1-\hour)*60)}
    \ifnum\hour<10\edef\hour{0\hour}\fi
    \ifnum\minute<10\edef\minute{0\minute}\fi
}
\pgfplotsset{
    /pgfplots/time coordinates in/.code={%
        \pgfkeysdef{/pgfplots/#1 coord trafo}{%
            \pgfplotslibdateplothour:minutetofloat##1.
        }
        \pgfkeysdef{/pgfplots/#1 coord inv trafo}{
            \pgfplotslibdateplofloattothour:minute##1.
        }
    }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents}{mylabel.csv}
X

 08:50:00
 09:20:00
 09:50:00
 10:20:00
 10:50:00
 11:20:00
 11:50:00
 12:20:00
 12:50:00
 
\end{filecontents}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{figure}[h!]
    \begin{tikzpicture}
    \centering
    \pgfplotsset{set layers}
    \begin{axis}[
    scale only axis,
    title={Závislosť koncentrácií na čase merania},
    axis y line*=left,
    xlabel={Čas merania [h]},
    ylabel={Koncentrácia $[mg / {Nm}_{ref}^3]$},
    ymin=120, ymax=860,
    ytick distance= 40,
    ymajorgrids=true,
     time coordinates in=x,
      xticklabel={\hour:\minute},
     xticklabels from table={mylabel.csv}{X}, 
     xmin={8:50},
     xmax={12:50},
      xticklabel style={rotate=90,anchor=east},
     width = 0.9 \textwidth,
     height = 0.8\textwidth,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
]
\end{axis}
\begin{axis}[
scale only axis,
axis y line*=right,
axis x line=none,
 ylabel={Obsah $O_2$ [\%]},
 ymin=6, ymax=16,
 ytick distance = 0.5,
 time coordinates in=x,
      xticklabel={\hour:\minute},
     xticklabels from table={mylabel.csv}{X}, 
     xmin={8:50},
     xmax={12:50},
     width = 0.9 \textwidth,
     height = 0.8\textwidth,
      xticklabel style={rotate=90,anchor=east},
 ]
 \end{axis}
    \end{tikzpicture}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document}

这是我编译时得到的结果: 在此处输入图片描述

我不明白为什么我的文件是从 8:50 开始的,而它却从 9:50 开始。

相关内容