5

I am using pgfplots to create a bar chart, with the x-axis representing different categories. Using symbolic x coords I can specify the labels for the categories. However, I am unable to use a mathematical expression to define the height of the bars (y coordinate). I suspect that this is related to the use of symbolic x coords.

Here's a minimum (non-)working example:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[symbolic x coords={a,b,c,d,e,f}]
        \addplot[ybar] coordinates {
            (a, 1*2)
            (b, 2*2)
            (c, 3*2)
            (d, 4*2)
            (e, 5*2)
            (f, 6*2)
        };
    \end{axis}
\end{tikzpicture}

\end{document}

The error I receive when compiling with pdflatex is:

Package PGF Math Error: Could not parse input ' 1*2' as a floating point number, sorry. The unreadable part was near '*2'.

Any help would be greatly appreciated.

Andrew Ho-Lee
  • 378
  • 3
  • 6

1 Answers1

5

You're right, the symbolic coordinates switches off the math parser because it wouldn't work with the symbolic coordinates, and it cannot be enabled selectively for the y coordinates.

It looks like you will have to work around it. Depending on how you generate the data for plotting, this might or might not be an acceptable solution - if you only need a couple of data points that you input by hand, this should work fine.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[xticklabels={a,b,c,d,e,f},
      xtick={1,...,6}, % To make sure the tick labels match the data points
      xticklabel style={text height=2ex}] % Make all letters the same height so they align properly
        \addplot[ybar] coordinates {
            (1, 1*2)
            (2, 2*2)
            (3, 3*2)
            (4, 4*2)
            (5, 5*2)
            (6, 6*2)
        };
    \end{axis}
\end{tikzpicture}

\end{document}

pseudo-symbolic x axis with pgfplots

Jake
  • 232,450