Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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

share|improve this answer
    
Thank you, this is a rather nice solution. –  Andrew Ho Feb 15 '11 at 23:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.