TikZpicture/pgfplots:如何将 mm:ss 格式(不带 hh:)的时间数据插入 x 轴?

TikZpicture/pgfplots:如何将 mm:ss 格式(不带 hh:)的时间数据插入 x 轴?

pgfplots想象使用/包的普通 x/y 图tikzpicture

如果是常规日期值(dd-mm-yyyy),通常应用上没有问题。但现在,我必须在 x 轴中添加 mm:ss(分钟:秒)格式的时间值,不带小时(hh:)。我该如何解决?


最小工作示例(MWE):

\documentclass{scrartcl}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
%------------------------------------------------------------------------------
%   date coordinates in = x,                     % set X-axis as date-axis
    xmin = 0,                                    % minimum-value of X
    xmax = 60,                                   % maximum value of X
    ymin = 0,                                    % minimum-value of Y
    ymax = 2200,                                 % maximum-value of Y
    xlabel={time [min]},                         % caption of X-axis
    ylabel = {pressure [mm H2O]},                % caption of Y-axis
%------------------------------------------------------------------------------
    ]
%------------------------------------------------------------------------------
    \addplot plot coordinates {(0,0)(60,2000)};  % just to show some line
%   \addplot table[x=time,y=pressure]{data.txt}; % the "real" data
%------------------------------------------------------------------------------
\end{axis}

\end{tikzpicture}

\end{document}

例如,所属数据表data.txt如下所示:

time    pressure
00:00   -3
00:01   50
00:01   170
01:02   300
02:02   500
03:03   700
04:03   750
05:04   800
10:04   1100
01:05   1200
18:05   1300
19:06   1400
20:06   1500
00:07   1600
30:07   1700
40:08   1800
50:08   1900
55:09   2000
59:09   2000
60:00   2000

我如何在图表的 x 轴内添加这样的时间格式?设置date coordinates in = x并没有给我带来解决方案 - 也许秒不被接受为日期......


编辑:也许这可能是一种方法?

\begin{filecontents}{mydata.csv}
X   Y1
 00:00.0   25
 00:00.5   25
 00:01.0   25.2
 00:01.5   25.7
 00:02.0   26.7
 00:02.5   27.7
 00:03.0   28.7
 00:03.5   29.7
 00:04.0   30.6
 00:04.5   31.6
 00:05.0   32.6
 00:05.5   33.6
 00:06.0   34.4
 00:06.5   35.4
 00:07.0   36.3
 00:07.5   37.2
 00:08.0   38.1
 00:08.5   38.9
 00:09.0   39.7
 00:09.5   40.5
 00:10.0   41.3
\end{filecontents}

\begin{filecontents}{mylabel.csv}
X
 00:00.0
 00:01.0
 00:02.0
 00:03.0
 00:04.0
 00:05.0
 00:06.0
 00:07.0
 00:08.0
 00:09.0
 00:10.0
\end{filecontents}

\documentclass[paper=a4,parskip]{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\pgfplotsset{/pgf/number format/use comma}

\begin{document}

    \begin{figure}
       \centering
       \begin{tikzpicture}
       \begin{axis}[width=0.6\textheight,      
       height=0.4\textheight,
       xlabel={Time [mm:ss.0]},
       ylabel={Temperature [Celsius]},
       ymajorgrids,
       xtick={0,2,...,100},
       xticklabels from table={mylabel.csv}{X},,
       xticklabel style={rotate=45,anchor=east},
       legend pos=outer north east,
       legend cell align=left,
       legend style={draw=none}]
       \addplot[color=black,mark=none] table [x expr=\coordindex,y=Y1] {mydata.csv};
       \legend{Graph}
       \end{axis}
       \end{tikzpicture}
    \end{figure}
\end{document}

结果截图:

在此处输入图片描述


不幸的是,当我将 mm:ss:0 格式更改为我想要的 mm:ss 格式时,出现了不同的错误...

答案1

好的,这个解决方案确实有效:


最小工作示例(MWE):

\begin{filecontents}{mydata.csv}
X   Y1
 00:00.0   25
 00:00.5   25
 00:01.0   25.2
 00:01.5   25.7
 00:02.0   26.7
 00:02.5   27.7
 00:03.0   28.7
 00:03.5   29.7
 00:04.0   30.6
 00:04.5   31.6
 00:05.0   32.6
 00:05.5   33.6
 00:06.0   34.4
 00:06.5   35.4
 00:07.0   36.3
 00:07.5   37.2
 00:08.0   38.1
 00:08.5   38.9
 00:09.0   39.7
 00:09.5   40.5
 00:10.0   41.3
\end{filecontents}

\begin{filecontents}{mylabel.csv}
X
 00:00.0
 00:01.0
 00:02.0
 00:03.0
 00:04.0
 00:05.0
 00:06.0
 00:07.0
 00:08.0
 00:09.0
 00:10.0
\end{filecontents}

\documentclass[paper=a4,parskip]{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\pgfplotsset{/pgf/number format/use comma}

\begin{document}

    \begin{figure}
       \centering
       \begin{tikzpicture}
       \begin{axis}[width=0.6\textheight,      
       height=0.4\textheight,
       xlabel={Time [mm:ss.0]},
       ylabel={Temperature [Celsius]},
       ymajorgrids,
       xtick={0,2,...,100},
       xticklabels from table={mylabel.csv}{X},,
       xticklabel style={rotate=45,anchor=east},
       legend pos=outer north east,
       legend cell align=left,
       legend style={draw=none}]
       \addplot[color=black,mark=none] table [x expr=\coordindex,y=Y1] {mydata.csv};
       \legend{Graph}
       \end{axis}
       \end{tikzpicture}
    \end{figure}
\end{document}

昨天没有用,因为我不知道mydata.csv在开始新的编译之前必须手动删除生成的内容...所以如果你在 TeX-GUI 中更改指示的值,它们不会在编译期间在生成的 mydata.csv 中自动更改...

相关内容