3

I'm trying to plot a vertical bar chart using pfgplots that has strings as labels for the bar charts. (This question has been asked before, but I'm hoping there is a cleaner way to do this now. Perhaps a different package?) Previous Question: pgfplots using strings *from data table* as x axis labels in bar chart

This is what my attempt looked like (MWE):

\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=x axis label,ylabel=y axis label]
\addplot [ybar] table [symbolic x coords=Month, y=Dozers, col sep=comma] {cnrldata.csv};
\end{axis}
\end{tikzpicture} \\
\end{document}

From this I of course get the error:

Package PGF Math Error: Could not parse input 'January' as a floating point number, sorry. The unreadable part was near 'January'.. ... y=Dozers, col sep=comma] {data.csv};

The data in the table looks like this:

Month, Dozers,
January, 0.85,
February, 0.7,

Hopefully there is a better solution to this now, but if not, I will delete this question since it is a duplicate.

  • Where does the input of May 14 come from? It's not in your data file or anything that I can see. Your code actually gives the error Package PGF Math Error: Could not parse input 'January' as a floating point :) – Andrew May 20 '17 at 1:06
  • Sorry, May 14 was what was actually in my data file, but I used January in my example file. I've amended the question. – Corus May 20 '17 at 1:32
3

It seems that you can do this using xtick labels and \xcoordindex. You first need to read the data into an "array", called \datatable below, using \pgfplotstableread. After this you just tell pgf to access the data in \datatable. This way you can produce

enter image description here

using the code:

\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.9}

\usepackage{filecontents}% write data file to make make MWE self contained 
\begin{filecontents}{dozers.csv}
Month, Dozers,
Jan, 0.85,
Feb, 0.7,
Mar, 0.6,
Apr, 0.75,
May, 0.8,
June, 0.9,
\end{filecontents}

\begin{document}

\pgfplotstableread[col sep=comma,]{dozers.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xlabel={Month},
    xtick=data,
    xticklabels from table={\datatable}{Month},
    ylabel={Dozers}]
    \addplot table [x expr=\coordindex, y={Dozers}]{\datatable};
\end{axis}
\end{tikzpicture} \\
\end{document}

EDIT

Following the solution to Using Labels from a File in PGFplot it seems that you don't have to read the file in first using \pgfplotstableread:

\documentclass[11pt]{article}
\usepackage{pgfplots}
%\usepackage{pgfplotstable}
\pgfplotsset{compat=1.9}

\usepackage{filecontents}
\begin{filecontents}{dozers.csv}
Month Dozers
Jan 0.85
Feb 0.7
Mar 0.6
Apr 0.75
May 0.8
June 0.9
\end{filecontents}

\begin{document}

%\pgfplotstableread[col sep=comma,]{dozers.csv}\datatable
\begin{tikzpicture}
\begin{axis}[
    ybar, ymin=0, ylabel=Dozers,
    xlabel=Month,
    xtick=data,
    xticklabels from table={dozers.csv}{Month},
    ]
    \addplot table [x expr=\coordindex, y=Dozers]{dozers.csv};
\end{axis}
\end{tikzpicture} \\
\end{document}

To get this to work I had to remove the commas from the data file. The output is essentially the same as above except that I added ymin-0.

  • Thank you! It works as expected. Seems strange to have to read it in first, but I guess I just have to live with it. – Corus May 20 '17 at 1:50
  • @Corus Actually, you don't have to read it in. See my edit. – Andrew May 20 '17 at 2:02
  • Thanks, this works also! One small nuance in doing this for my case, is that it must be saved without commas, which can be done in excel. However, any spaces in the labels will cause an error. For example if the Month column contained May 10 it would create an error. Not a big deal for me, but something to consider when using the second portion of your solution. – Corus May 20 '17 at 2:57

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.