[Tex/LaTex] Symbolic Labels in pgfplots Bar Chart

bar chartpgfplots

In the following code, why do I have to type the x-axis labels explicitly in the axis options and not just use the labels from the coordinates data? Is there a way to just use the data from the coordinates? This wold be really inconvenient specially for large data sets..

\documentclass{article}
  \usepacage{pgfplots}
  \begin{document}
    \begin{center}
    \begin{tikzpicture}
      \begin{axis}[
        width=12cm,
        height=5cm,
        ybar,
        axis y line=left,
        axis x line=middle,
        enlargelimits=0.15,
        ylabel={Score/5},
        ymin = 0,
        ymax = 5,
        symbolic x coords={a,b,c,d,%
                e,f,g,h},
        xtick=data,
        nodes near coords align={vertical},
        x tick label style={rotate=90,anchor=east},
      ]
        \addplot[fill=maroon!30] coordinates {  (f2011,4.1)
                    (a,4.2) (b,4.2) (c,3.9)
                    (d,4.0) (e,4.1) (f,4.0)
                    (g,4.0) };
    \end{axis}
  \end{tikzpicture}
 \end{center}
\end{document}

Best Answer

You can do it if you read the coordinates from a table:

Code

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{pgfplotstable}
\usepackage{amsmath}

\pgfplotstableread{
sym y
a 4.2
b 4.2
c 3.9
d 4.0
e 4.1
f 4.0
g 4.0
}\mypoints

\begin{document}

\begin{tikzpicture}
    \begin{axis}
    [   axis y line=left,
        axis x line=middle,
        enlargelimits=0.15,
        ylabel={$\frac{\text{Score}}{5}$},
        ymin = 0,
        ymax = 5,
        ybar,
        xticklabels from table={\mypoints}{sym},
        xtick=data,
    ]
        \addplot[fill=red!50!black] table [x expr=\coordindex] {\mypoints};
    \end{axis}
\end{tikzpicture}

\end{document}

Output

enter image description here

Related Question