2

Here is my code -

\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}

The data is in the form of a txt file and here are the top 4 rows -

x y
 60 -3131.84
 51 -6275.58
 59 -4578.68
 42 -9346.83
2
  • 1
    Haven't tested anything, but why the col sep=comma when the file uses spaces as column separator? Dec 23 '20 at 22:53
  • Thanks @TorbjørnT. I made the correction Dec 23 '20 at 22:58
1

There are three problems with

table[x = x, y = z][col sep = “white space”] {abc.txt}; 

First off, I get an error saying

! Package pgfplots Error: Could not read table file '[' in 'search path=.'

What happens there is that when pgfplots reaches the end of the first bracket pair, the next thing should be a filename (or macro for a loaded table). But the next thing it sees is the second bracket, so you get the above error. If you need to specify the col sep, do it in the same brackets as where you specify the column.

Second, there is no white space value for the col sep key. There is one called space, but that is the default, so you don't actually need to specify it. To sum up these first two points, use either

table[x = x, y = z, col sep=space]

or

table[x = x, y = z]

Finally, having fixed the above, I get the error

Package pgfplots Error: Sorry, could not retrieve column 'z' from table

which points to the minor mistake of having table[x=x, y=z], while the column name actually used in the file is y. I.e., you need table[x=x, y=y].

1
  • 1
    By the way, if you don't specify the columns, pgfplots will use the first column for the x-values and the second column for the y-values, so in your case you could just do \addplot table{abc.txt}; Dec 23 '20 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.