Corus
Corus

Reputation: 53

LaTeX pgfplots use strings for barchart label from .csv

I'm trying to use data from a .csv file to create a barchart with labels that are non-numeric. I've looked through a few older examples, but they seem large and clunky. I'm hoping there's a better way. Here's what I have so far as a 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 'May 14' as a floating point number, sorry. The unreadable part was near 'May 14'.. ... y=Dozers, col sep=comma] {data.csv};

The data in the table looks like this:

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

Upvotes: 0

Views: 4442

Answers (1)

Henri Menke
Henri Menke

Reputation: 10939

Your usage of symbolic x coords is wrong. Read the manual.

Tip: You are more likely to get such questions answered on TeX.SX.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$ axis label},
    ylabel={$y$ axis label},
    symbolic x coords={January,February,March,April,May},
    ]
    \addplot [ybar] table [x=Month, y=Dozers, col sep=comma] {
      Month, Dozers
      January, 0.85
      February, 0.7
      March,    0.6
      April,    0.9
      May,      0.4
    };
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Upvotes: 1

Related Questions