7

I would like to have a grid in the y axis although the axis y line is not drawn, either by setting axis y line=none or hide y axis. In the MWE below the horizontal line along y=0 is not drawn when I use the extra y ticks method described in one of the answers here. How could it work when the y axis is not displayed?

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    extra y ticks       = 0,
    extra y tick style  = { grid = major },
    domain=0:360,
    hide y axis,
    ]
    \addplot {sin(x)};
  \end{axis}
\end{tikzpicture}
\end{document}
gypaetus
  • 3,162

1 Answers1

13

The key hide y axis completely disables all ticks and lines, extra or otherwise. Instead, you can stop the axis line from being drawn by setting

separate axis lines,
y axis line style= { draw opacity=0 }

The separate axis lines is necessary to be able to assign the line style only to the y axis, since usually the axes are drawn with a single path. Note that you have to use draw opacity=0 to hide the y axis, the usual approach draw=none doesn't work here.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    domain=0:360,
    ytick=0,
    separate axis lines,
    y axis line style= { draw opacity=0 },
    ymajorgrids,
    tick pos = left
    ]
    \addplot {sin(x)};
  \end{axis}
\end{tikzpicture}
\end{document}

In case you want to do away with the tick mark and the tick label, the easiest thing might be to use one of the approaches from How can I add a zero line to a plot? to draw the zero line without the tick/grid mechanism.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    domain=0:360,
    hide y axis,
    before end axis/.code={
      \draw [/pgfplots/every axis grid] ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
    },
    axis lines*=left
    ]
    \addplot {sin(x)};

  \end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • Thanks! that works great except that I would like to remove the tick and ytick={} yticklabel=\empty does not do it, – gypaetus Jul 31 '13 at 17:26
  • 1
    @MigueldeVal-Borro: Yeah, to get rid of the tick mark you would have to set major tick length=0pt, but that would apply to the x axis as well. I've edited my answer to show a different approach. – Jake Jul 31 '13 at 18:00
  • Excellent! Since I am using the extra x ticks method for the x axis and would like to have the same style, is the default extra tick style gray, very thin or gray, ultra thin ? – gypaetus Jul 31 '13 at 19:34
  • 1
    @MigueldeVal-Borro: The default style is thin,black!25. You can also use the actual style that's used to draw the grid lines by adding /pgfplots/every axis grid to the \draw options. – Jake Jul 31 '13 at 20:10
  • 1
    With the release of PGFPlots v1.13 the bug has been fixed and now also y axis line style={draw=none} works fine. – Stefan Pinnow Jan 10 '16 at 06:57
  • @Jake I am sorry for asking this question here, but how can I remove a major or minor grid line at a specific x tick? For example, I need to remove the major grid line at x=2000. – Diaa Apr 06 '17 at 20:16