[Tex/LaTex] Cannot import data from csv file with pgfplots

pgfplotspgfplotstable

I need to import some data from a csv file, but I get Package pgfplots Error: Table 'Data1.csv' appears to have too many columns in. Other posts related to this are not helpful.

My code is here:

\RequirePackage{filecontents}
\begin{filecontents}{Data1.csv}
Periodo,spot,CI1,CI2,CI3,CI4
ene 13,1.30,1.42,1.19,1.48,1.12
feb 13,1.40,1.52,1.28,1.59,1.21
mar 13,1.20,1.31,1.09,1.37,1.03
abr 13,1.60,1.73,1.47,1.81,1.39
may 13,1.31,1.43,1.20,1.49,1.13
jun 13,0.94,1.03,0.84,1.08,0.79
jul 13,1.23,1.34,1.12,1.40,1.06
ago 13,1.40,1.52,1.28,1.59,1.21
sep 13,1.65,1.78,1.52,1.86,1.43
oct 13,1.81,1.95,1.67,2.04,1.58
nov 13,2.14,2.29,1.98,2.40,1.87
dic 13,2.07,2.22,1.92,2.33,1.81
ene 14,1.66,1.80,1.53,1.88,1.45
feb 14,1.71,1.84,1.57,1.93,1.49
mar 14,1.58,1.71,1.45,1.79,1.37
abr 14,2.08,2.23,1.92,2.33,1.82
may 14,2.05,2.20,1.89,2.30,1.79
jun 14,1.95,2.09,1.80,2.19,1.70
jul 14,2.10,2.26,1.95,2.36,1.84
ago 14,2.63,2.81,2.45,2.94,2.32
sep 14,2.36,2.53,2.19,2.65,2.08
oct 14,2.28,2.44,2.11,2.56,2.00
nov 14,1.41,1.53,1.29,1.60,1.22
dic 14,1.73,1.87,1.59,1.95,1.51
ene 15,1.44,1.57,1.32,1.64,1.25
feb 15,1.81,1.96,1.67,2.05,1.58
mar 15,2.04,2.19,1.89,2.30,1.79
abr 15,2.11,2.27,1.96,2.37,1.85
may 15,1.98,2.13,1.83,2.23,1.73
jun 15,1.93,2.08,1.78,2.17,1.69
jul 15,2.12,2.28,1.97,2.39,1.86
ago 15,2.23,2.39,2.06,2.50,1.95
sep 15,1.81,1.95,1.67,2.04,1.58
oct 15,1.76,1.90,1.62,1.98,1.53
nov 15,1.18,1.29,1.07,1.35,1.01
dic 15,1.12,1.23,1.01,1.28,0.96
ene 16,0.72,0.80,0.63,0.84,0.59
feb 16,0.79,0.88,0.70,0.92,0.66
mar 16,0.75,0.84,0.67,0.88,0.63
abr 16,0.84,0.93,0.74,0.97,0.70
may 16,0.93,1.03,0.84,1.08,0.79
jun 16,1.43,1.55,1.30,1.62,1.23
jul 16,1.46,1.59,1.34,1.66,1.27
ago 16,1.48,1.60,1.36,1.68,1.28
sep 16,1.16,1.26,1.05,1.32,0.99
oct 16,0.64,0.72,0.56,0.75,0.53
nov 16,0.95,1.04,0.85,1.09,0.80
dic 16,1.26,1.37,1.14,1.43,1.08
ene 17,1.30,1.42,1.19,1.48,1.12
feb 17,1.30,1.41,1.18,1.48,1.12
mar 17,1.53,1.66,1.40,1.73,1.33
abr 17,1.65,1.78,1.51,1.86,1.43
may 17,1.98,2.13,1.83,2.23,1.73
jun 17,2.26,2.43,2.10,2.54,1.99
jul 17,2.17,2.33,2.02,2.44,1.91
ago 17,1.44,1.56,1.31,1.63,1.24
sep 17,1.27,1.38,1.15,1.44,1.09
oct 17,1.48,1.61,1.36,1.68,1.28
nov 17,1.31,1.42,1.19,1.49,1.13
dic 17,1.08,1.18,0.98,1.24,0.92

\end{filecontents}
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[british]{babel}
\usepackage{graphicx}
\usepackage{pgfplots,tikz}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{Data1.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    xlabel=Periodo,
    ylabel=Inflacion,
    xticklabels from table={Data1.csv}{Periodo},
    xtick=data
]
\addplot table [x expr=\coordindex, y=spot, col sep=comma] {\datatable};
\end{axis}
\end{tikzpicture}

\end(document}

What is the problem?

Best Answer

You need to tell pgfplots the table is comma-separated when it's read

\pgfplotstableread[col sep=comma]{Data1.csv}\datatable

not when its used.

(I'd not normally read into a macro in a separate step but would use \addplot table [x expr=\coordindex, y=spot, col sep=comma] {Data1.csv};, but the outcome is the same.)


I note that the line

xticklabels from table={Data1.csv}{Periodo},

is also wrong as this forces re-reading of the table and as you've not set col sep globally it will again fall over trying to use spaces not commas. As you've read the table into \datatable you can just use

xticklabels from table={\datatable}{Periodo},

instead.

Related Question