0

So I have this "grouped" bar plot as shown below.

enter image description here

I want the x-ticks to be labeled

-2, -1, 0, 1, 2, 3, 4, 5

How can I achieve this? Right now only -2 and -1 shows up

\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=6.6cm,compat=1.7}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
    ybar,
    enlargelimits=0.15,
    ylabel={\# Number of students}, % the ylabel must precede a # symbol.
    xlabel={Difference},
    symbolic x coords={mTwo, mOne, Zero, pOne, pTwo, pThree, pFour, pFive}, % these are the specification of coordinates on the x-axis.
    xtick=data,
     nodes near coords, % this command is used to mention the y-axis points on the top of the particular bar.
    nodes near coords align={vertical},
    xticklabels = {$-2$,$-1$,$0$,$1$,$2$,$3$,$4$,$5$}
    ]
\addplot coordinates { (mTwo,2) (mOne,1) };
\addplot coordinates { (Zero,5) };
\addplot coordinates { (pOne,3) (pTwo,15) (pThree,10) (pFour,5) (pFive,1) };

\end{axis}
\end{tikzpicture}
\end{document}
N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114
  • 2
    xtick=data only looks at the first \addplot command. I suspect this question is related: https://tex.stackexchange.com/questions/161321/not-all-tick-labels-are-shown-when-using-xtick-data – rbrignall Feb 07 '20 at 12:33

1 Answers1

2

As @rbrignall said, your xtick only takes data from the first \addplot in the axis, which results in only two ticks and thus two tick labels. I'm not quite sure why you need symbolic coordinates here (see below for simpler code without them), but in order to see all ticks, you can pass xtick the value mTwo, mOne, Zero, pOne, pTwo, pThree, pFour, pFive. Since we also need to feed it to symbolic x coords, I suggest to store it in a PGF key, for instance /pgfplots/my coords, and setting both /pgfplots/symbolic x coords and /pgfplots/xtick from this value using \pgfkeysvalueof{/pgfplots/my coords}β€”in application of the DRY principle.

I also set bar shift=0pt, otherwise each new \addplot horizontally shifts the coordinate system due to the use of ybar, which is presumably not what you want here.

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

\begin{document}
\begin{tikzpicture}

\begin{axis}[
    ybar, bar width=0.6cm,
    enlargelimits=0.15,
    ylabel={\# Number of students},
    xlabel={Difference},
    % This is used to avoid repeating the list of symbolic coordinates
    my coords/.initial={mTwo, mOne, Zero, pOne, pTwo, pThree, pFour, pFive},
    symbolic x coords/.expanded={\pgfkeysvalueof{/pgfplots/my coords}},
    xtick/.expanded={\pgfkeysvalueof{/pgfplots/my coords}},
    nodes near coords,
    nodes near coords align={vertical},
    xticklabels = {$-2$, $-1$, $0$, $1$, $2$, $3$, $4$, $5$},
    bar shift=0pt,
    ]
\addplot coordinates { (mTwo,2) (mOne,1) };
\addplot coordinates { (Zero,5) };
\addplot coordinates { (pOne,3) (pTwo,15) (pThree,10) (pFour,5) (pFive,1) };
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

P.S.: pgfplots compat level 1.7 is old, I bumped it to 1.16.

Without symbolic coordinates

The same plot can be drawn in a simpler way without symbolic coordinates:

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

\begin{document}
\begin{tikzpicture}

\begin{axis}[
    ybar, bar width=0.6cm,
    enlargelimits=0.15,
    ylabel={\# Number of students},
    xlabel={Difference},
    xtick={-2,...,5},
    nodes near coords,
    nodes near coords align={vertical},
    bar shift=0pt,
    ]
\addplot coordinates { (-2,2) (-1,1) };
\addplot coordinates { (0,5) };
\addplot coordinates { (1,3) (2,15) (3,10) (4,5) (5,1) };
\end{axis}

\end{tikzpicture}
\end{document}

(same output as with the first example).

frougon
  • 24,283
  • 1
  • 32
  • 55