2

I have a csv file like:

column1,column2
string1,value1
string2,value2
…

Values are integers, strings are alphanumeric.

I tried to plot it, with strings on x axis and values on y axis. After some tries leading to errors like Could not parse input "string1" as a floating point number, sorry. The unreadable part was near…, based on some questions (like here, here, here, here or here), i finally found this only way to get the things works, (see also the doc, page 363):

\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={string1,string2,string3,…},
    minor tick num=1,
]
\addplot table [x=column1,y=column2] {csvfile.csv}
\end{axis}
\end{tikzpicture}

Obviously, the non-numerical data needs to be write directly in the graph definition.

How can i have the same in a DRY way, i.e. have for only source of data the CSV file given as \addplot parameter ?

aluriak
  • 131

1 Answers1

1

Thanks to torbjørn-t comment, i finally found this answer and the xticklabels from table={<table or file name>}{<column name>} option.

Here is the final working code:

\pgfplotstableread[col sep=comma]{path/to/csv}\datatable
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        xtick=data,
        xticklabels from table={\datatable}{column1},
    ]
    \addplot table [col sep=comma, x expr=\coordindex, y=column2] {path/to/csv};
    \end{axis}
\end{tikzpicture}
aluriak
  • 131