这是我的代码 -
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{tikz}
\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\begin{axis}[yticklabels=\empty, xticklabels=\empty]
\addplot
% add a plot from table; you select the columns by using the actual name in
% the .csv file (on top)
table[x = x, y = z][col sep = “white space”] {abc.txt};
\legend{Plot}
\end{axis}
\end{tikzpicture}
\caption{Plots}
\end{center}
\end{figure}
数据以 txt 文件的形式呈现,以下是前 4 行 -
x y
60 -3131.84
51 -6275.58
59 -4578.68
42 -9346.83
答案1
有三个问题
table[x = x, y = z][col sep = “white space”] {abc.txt};
首先,我收到一条错误消息
! Package pgfplots Error: Could not read table file '[' in 'search path=.'
那里发生的事情是,当pgfplots
到达第一个括号对的末尾时,下一个应该是文件名(或已加载表格的宏)。但它看到的下一个是第二个括号,所以你会得到上面的错误。如果你需要指定col sep
,请在与指定列相同的括号中执行此操作。
white space
其次,该键没有值col sep
。有一个名为 的值space
,但这是默认值,因此您实际上不需要指定它。总结前两点,使用
table[x = x, y = z, col sep=space]
或者
table[x = x, y = z]
最后,修复了上述问题后,我得到了错误
Package pgfplots Error: Sorry, could not retrieve column 'z' from table
这指出了一个小错误,即table[x=x, y=z]
,而文件中实际使用的列名是y
。即,您需要table[x=x, y=y]
。