5

I'm starting to learn pgf-plot and this is my first plot:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    ymin=0, ymax=70,
    width=9.5cm,
    symbolic x coords={a,b,c,d,e},
    xtick=data,
    bar width=15pt,
    axis lines*=left,
    ytick={0,10,...,70},
    xticklabel style={text height=1.5ex},
    ymajorgrids, 
    ]
    \addplot[fill=gray!40] coordinates {
        (a,54)
        (b,60)
        (c,62)
        (d,58)
        (e,51)
    };
\end{axis}
\end{tikzpicture}
\end{document}

Now I want to draw horizontal lines from every yticks to the other end of the plot. I tried with xbar interval but it didn't work. How should I do it?

Edit: nevermind, found it. It was the xmajorgrids option. Now what I can't do is to remove the first line from the horizontal grid (first from above, the hline at y=70). Is it possible?

Jeremy
  • 53

1 Answers1

4

This is one possibility:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    ymin=0, ymax=70,
    width=9.5cm,
    symbolic x coords={a,b,c,d,e},
    xtick=data,
    bar width=15pt,
    axis lines*=left,
    ytick={0,10,...,60},                  %changed code
    xticklabel style={text height=1.5ex},
    ymajorgrids,
    extra y ticks=70,                     %new code
    extra y tick style={grid=none}        %new code
    ]
    \addplot[fill=gray!40] coordinates {
        (a,54)
        (b,60)
        (c,62)
        (d,58)
        (e,51)
    };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

What I did was to remove the value you didn't want from your ytick option and then add an extra tick with the removed value but with a no grid style.

d-cmst
  • 23,095
  • WOuld it also be possible to remove any line with this method? Say, y=40? – Jeremy Dec 09 '13 at 15:41
  • yes, in the worst case scenario just set the full list of ticks ytick={0,10,20,30,50,60,70}, taking out the one you don't want to be in the grid and readding it as an extra tick. – d-cmst Dec 09 '13 at 16:27