8

I'm making a ybar graph (vertical bars) where the y axis maximum reaches 150+, but a lot of values are 0's. I would like the final graph would not show a minimum bar in this 0 values, since we this values it can easily be mistaken with a low value (1,2, etc).

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,
      scale only axis,
      height=0.5\textwidth,
      width=1.2\textwidth,
      ylabel={# Dots},
      nodes near coords,
      xlabel={Variation},
      xticklabel style={
        rotate=90,
        anchor=east,
      },
      enlarge x limits={abs value={3}},
      ]
      \addplot table [
        x=grade,
        y=value,
      ] {
grade   value
-11 0
-10 0
-9  0
-8  0
-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
8   0
9   0
10  0
11  0
      };
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

My graph:

enter image description here

1 Answer 1

10

You can use

 y filter/.expression={y==0 ? nan : y}

in the options of \addplot.

\documentclass{article}

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

\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis} [
      % general
      ybar,
      scale only axis,
      height=0.5\textwidth,
      width=1.2\textwidth,
      ylabel={\# Dots},
      nodes near coords,
      xlabel={Variation},
      xticklabel style={
        rotate=90,
        anchor=east,
      },
      %enlarge x limits={abs value={3}},
      ]
      \addplot+[y filter/.expression={y==0 ? nan : y}] table [
        x=grade,
        y=value,
      ] {
grade   value
-11 0
-10 0
-9  0
-8  0
-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
8   0
9   0
10  0
11  0
      };
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

6
  • This solution is only available in version 1.12. Is there another way, valid for previous versions? Commented Sep 26, 2015 at 12:49
  • @FernandoCésar: It is available also in older versions. Please check. However, it is better to update your pgfplots as it has become very juicy later on. :)
    – user11232
    Commented Sep 26, 2015 at 12:52
  • I get an error and the example in the manual uses that version. Package pgfkeys Error: I do not know the key '/pgfplots/y filter/.expression' , to which you passed 'y==0 ? nan : y', and I am going to ignore it. Perhaps yo u misspelled it. Commented Sep 26, 2015 at 16:38
  • @FernandoCésar: What is the version of your pgfplots? Add \listfiles some where in the code and check in the log file. But it is better to update.
    – user11232
    Commented Sep 26, 2015 at 23:23
  • I found this pgfplots.sty 2014/08/04 v1.11 Commented Sep 27, 2015 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .