1

I am new to LaTeX and am hoping to use it for plotting graphs for my thesis. For the most part I find it easy to use. I have no problem plotting a function, but I cannot get it to plot from an external csv file. I've seen numerous tutorials and examples and I've even copied and pasted code from those, but I can't get anything to work. The error I get is:

Package pgfplots Error: Could not read table file 'HeatSources.csv' in 'search path=.'

I think part of the issue I am having is not getting a complete example. Tutorials and examples posted on here typically show a small data set defined at the beginning of the tex file with no complete instructions on how the syntax should look for using a file saved on the computer. I've included the entire code for one of my tutorials below. The plot never shows up and I always get the error I mentioned above ^^. Can anyone show me the EXACT syntax for reading in a csv file including what the file path should look like? Or does anyone know if I am waaaay off base and there is something else wrong with the code?

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\usepackage{csvsimple}


\pgfplotsset{compat=newest} % Allows to place the legend below plot
\usepgfplotslibrary{units} % Allows to enter the units nicely

\sisetup{
  round-mode          = places,
  round-precision     = 2,
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
xtick distance = 5,
ytick distance = 1,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
]


% add a plot using a file where values are separated by spaces
\addplot[
smooth,
thin,
red,
dashed
] table[col sep=comma, x=Time from start of January 1 (h), y=Transformer (W)]
{HeatSources.csv}{/C:/Users/kayla/OneDrive/Documents/Plots and graphics/HeatSources.csv};

\end{axis}
\end{tikzpicture}
\end{document}

Oh and the first few rows of the csv file look like this:

Time from start of January 1 (h),Transformer (W),Elec equipment + lighting (W),Water tanks (W)
0.042,5.4,1194.5,43.75690471
0.125,150,1035,44.32023815

Thank you!

Kayla
  • 21
  • 1
  • 4
  • You mentioned in your other duplicate of this post that you compile with overleaf. If you want to use this online tool, you need to make your `.csv` file available there, overleaf cannot access the files on your hard drive. Overleaf has an option to upload files. – samcarter_is_at_topanswers.xyz Aug 04 '20 at 22:03
  • Then the syntax is incorrect. It should be `\addplot[ smooth, thin, red, dashed ] table[col sep=comma, x=Time from start of January 1 (h), y=Transformer (W)] {HeatSources.csv};` - notice only one curly bracket with the file name and not two (assuming you will upload the `.csv` in the same folder as your main tex file) – samcarter_is_at_topanswers.xyz Aug 04 '20 at 22:04
  • @samcarter_is_at_topanswers.xyz It worked! Thank you so so so much...that was an embarrassingly simple fix hahaha but I'm so glad it works! Thanks again! – Kayla Aug 05 '20 at 13:28

0 Answers0