2

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}
2

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

  • 1
    Hmm, that coordinate drawing function might need a strut or similar ;-) – daleif Oct 31 '15 at 7:50
  • @daleif: I'm sorry, I don't understand what you are saying? – Tom Bombadil Oct 31 '15 at 7:54
  • 1
    Look at the letters under the bars, they are jumping up and down with out a common baseline. Looks a little odd. Seemingly because they don't have the same hight. – daleif Oct 31 '15 at 7:56
  • @daleif: Ah, yes, that's probably why they were turned sideways in the original question ;-) – Tom Bombadil Oct 31 '15 at 7:58
  • 2
    Adding something like xticklabel style={text height=1.5ex} or xticklabel style={font=\strut} should fix that. – Torbjørn T. Oct 31 '15 at 8:16

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.