4

I'm doing a graph with vertical bars (ybar) where each bar represents not a number (1) but a class ([-0.5,0.5]).

I would prefer that tick labels on the x axis appeared near the ticks marks, so marking the limits of each interval/class and not how they actually appear, at the center of the bar. How can I do this?

My code:

\documentclass{article}

% ---------------------------------- tikz 
\usepackage{pgfplots}          % to print charts
\pgfplotsset{compat=1.8}
%


\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis} [
      % general
      ybar interval,
      scale only axis,
      height=0.5\textwidth,
      width=\textwidth,
      ylabel={Dots numbers},
      xlabel={Variation},
      xticklabel style={
        rotate=90,
        anchor=east,
      },
      minor xtick={-6,...,6},
      enlargelimits=false,
      ]
      \addplot table [
        x=grade,
        y=value,
      ] {
grade   value
-7  0
-6  0
-5  3
-4  1
-3  2
-2  15
-1  11
0   179
1   8
2   1
3   0
4   1
5   2
6   0
7   0
      };
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

And the graph:

ybar graph

3

You can apply a shift to the labels. I'd like to propose you, however, another approach presenting the labels as intervals. Below I show both alternatives:

\documentclass{article}

% ---------------------------------- tikz 
\usepackage{pgfplots}          % to print charts
\usepackage{etoolbox}
\pgfplotsset{compat=1.11}
%


\begin{document}

\begin{tikzpicture}
    \begin{axis} [
      % general
      ybar interval,
      scale only axis,
      height=0.5\textwidth,
      width=\textwidth,
      ylabel={Dots numbers},
      xlabel={Variation},
      x tick label as interval=true,
      xticklabel={
$[\pgfmathprintnumber{\tick},\pgfmathprintnumber{\nexttick}\pgfmathparse{int(\tick)}\ifnum\pgfmathresult=6\relax]\else)\fi$
        },
      xtick=data,
      xticklabel style={
        rotate=90,
        anchor=east,
      },
      minor xtick={-6,...,6},
      enlargelimits=false,
      ]
      \addplot table [
        x=grade,
        y=value,
      ] {
grade   value
-7  0
-6  0
-5  3
-4  1
-3  2
-2  15
-1  11
0   179
1   8
2   1
3   0
4   1
5   2
6   0
7   0
      };
    \end{axis}
\end{tikzpicture}


\begin{tikzpicture}
    \begin{axis} [
      % general
      ybar interval,
      scale only axis,
      height=0.5\textwidth,
      width=\textwidth,
      ylabel={Dots numbers},
      xlabel={Variation},
      xticklabel style={
        rotate=90,
        anchor=south east,
        yshift=0.5*\pgfplotbarwidth
      },
      minor xtick={-6,...,6},
      enlargelimits=false,
      ]
      \addplot table [
        x=grade,
        y=value,
      ] {
grade   value
-7  0
-6  0
-5  3
-4  1
-3  2
-2  15
-1  11
0   179
1   8
2   1
3   0
4   1
5   2
6   0
7   0
      };
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks. I had thought of that first approach, but (without seeing it) it didn't seem as easy to read. However, that specific solution has a problem, since the last value of each interval is repeated: -6 appears in two classes. Even if corrected [-7,-6[,[-6,-5[, etc., the last interval class would have to be different (to close the set, [6,7]. Would that be possible? I also find it strange that this specific style (ybar interval) doesn't address this auto of the box (I guess there is always space for improvement!) – Fernando César Sep 24 '15 at 0:26
  • @FernandoCésar Yes, it is possible. Please see my updated answer. – Gonzalo Medina Sep 24 '15 at 2:55

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.