使用外部文件绘制日期数据的问题

使用外部文件绘制日期数据的问题

使用日志进行统计,我有大量包含两列的数据,像这样(dd-mm-aaaa hh-mm 浮点数)

27-04-2018 04:43    7.78269552
27-04-2018 04:44    7.901522832
27-04-2018 04:45    7.754261587
27-04-2018 04:46    7.823737353
27-04-2018 04:47    7.799246215
27-04-2018 04:48    7.739765084
27-04-2018 04:49    7.776174494
27-04-2018 04:50    7.71269577
27-04-2018 04:51    7.682809896
27-04-2018 04:52    7.66061104
27-04-2018 04:53    7.727415335
27-04-2018 04:54    7.679670753
27-04-2018 04:55    7.677914655

通过尝试绘制这些数据,我写下了以下内容:

\documentclass[12pt, openany]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgf}

\usetikzlibrary{arrows}
\usepgfplotslibrary{dateplot} 

\tikzset{every picture/.style={execute at begin picture={\shorthandoff{:;!?};}}}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
xticklabel=\day. \hour:\minute
]
\addplot table [mark=none] {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}

我知道有些包或信息可能不是该代码所必需的,但这只是我剪切的文档的一个示例。我仍然可以用逗号分隔两列,但我仍然收到该消息:

! Package PGF Math Error: Could not parse input '04:44' as a floating point number, sorry. The unreadable part was near ':44'..

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.

希望我的问题足够清楚。我已经查看了许多其他问题,但都没有答案。

谢谢 !

答案1

因为你的列是用空格分隔的,所以pgfplots数据文件中的列,而不是两列。如果两列之间有制表符,您可以尝试使用

\addplot[mark=none] table[col sep=tab]  {data.txt};

或者,使用逗号代替,例如

27-04-2018 04:43,    7.78269552

然后使用col sep=comma来告知pgfplots列分隔符:

\addplot[mark=none] table[col sep=comma]  {data.txt};

(您[mark=none]来错地方了,这是一个与绘图有关的选项,而不是表格阅读。)

enter image description here

\documentclass[12pt, openany]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{pgfplots} % loads tikz which loads pgf

\usetikzlibrary{arrows}
\usepgfplotslibrary{dateplot} 

\tikzset{every picture/.style={execute at begin picture={\shorthandoff{:;!?};}}}
\pgfplotsset{compat=1.16}


%%%%%%%% just for example %%%%%%%%%%%
% filecontents makes the example self-contained
\usepackage{filecontents}
\begin{filecontents*}{mydata.dat}
27-04-2018 04:43,    7.78269552
27-04-2018 04:44,    7.901522832
27-04-2018 04:45,    7.754261587
27-04-2018 04:46,    7.823737353
27-04-2018 04:47,    7.799246215
27-04-2018 04:48,    7.739765084
27-04-2018 04:49,    7.776174494
27-04-2018 04:50,    7.71269577
27-04-2018 04:51,    7.682809896
27-04-2018 04:52,    7.66061104
27-04-2018 04:53,    7.727415335
27-04-2018 04:54,    7.679670753
27-04-2018 04:55,    7.677914655
\end{filecontents*}
%%%%%%%%%%---------%%%%%%%%%%%%%%%%

\begin{document}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
xticklabel=\day. \hour:\minute,
xticklabel style={rotate=45,anchor=north east} % one option for making the ticklabels readable
]
\addplot[mark=none] table[col sep=comma]  {mydata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容