Customize length of y major grids in pgfplots

Using some new features that were introduced since your question it is now relatively easy to achieve the desired result ...

For more details please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=-0.8,
        xmax=0,
        ymin=-100,
        ymax=25,
        legend style={
            draw=none,
            fill=none,
        },
        % ---------------------------------------------------------------------
        %%% these are the magic lines to get what you want
        % only show axis lines on left and bottom
        axis lines=left,
        % with the above command arrows are by default shown on the axis lines
        % which we don't want here
        x axis line style={-},
        y axis line style={-},
        % the axis lines should be shifted (like the Tufte style)
        axis line shift=10pt,
        % and we want to show major grid lines for the y axis
        ymajorgrids=true,
        % the y tick distance showed be increased ...
        ytick distance=50,
        % ---------------------------------------------------------------------
        tick align=outside,
        every axis legend/.append style={at={(0.9,0.5)}},
        legend cell align=right,
        no markers,
        smooth,
    ]
        \def\yticks{\pgfkeysvalueof{/pgfplots/ytick}}

        \foreach \datafile in {50,500} {
            \addplot table [
                skip first n=2,
                x expr=\thisrowno{0} + 0.412,
                y expr={
                    ( 1000000 * \thisrowno{1} ) / sqrt ( \datafile  / 1000 )
                },
            ] {\datafile.ocw};
                \addlegendentryexpanded{\SI{\datafile}{\milli\volt\per\second}};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code


This does the trick.

enter image description here

  • the y ticks are saved in the variable \yticks
  • the white background of the caption is removed to prevent hiding of the grid
  • \pgfplotsinvokeforeach is used instead of foreach because of the axis environment (see for example this question)
  • the lines are drawn with \draw using extremal x-coordinates from data.

Complete code:

\documentclass{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{siunitx

\pgfplotsset{compat = newest}

\pgfplotsset{
  every axis legend/.append style =
    {
      cells = { anchor = east },
      draw  = none
    },
}

\makeatletter
\pgfplotsset{
  tufte axes/.style =
    {
      after end axis/.code =
        {
          \draw ({rel axis cs:0,0} -| {axis cs:\pgfplots@data@xmin,0})      -- ({rel axis cs:0,0}  -| {axis cs:\pgfplots@data@xmax,0});
          \draw ({rel axis cs:0,0} |- {axis cs:0,\pgfplots@data@ymin})            -- ({rel axis cs:0,0}  |-{axis cs:0,\pgfplots@data@ymax});
                 },
      axis line style = {draw = none},
      tick align      = outside,
      tick pos        = left
    },
      execute at begin axis={ 
            \expandafter\pgfplotsinvokeforeach\expandafter{\yticks}{\draw[gray,thin] ({axis cs:\pgfplots@data@xmin,#1}) -- ({axis cs:\pgfplots@data@xmax,#1}); }
        },
    every axis y grid/.append style={red},
}
\makeatother


\begin{document}

\def\yticks{-100,-50,0} % TICKS DEFINED MANUALLY
\begin{tikzpicture}
  \begin{axis}%
       [
    legend style={fill=none}, % SO THAT THE LINE IS NOT HIDDEN BY THE LEGEND BA
      tufte axes,
      every axis legend/.append style = {at = {(0.9,0.5)}}
    ]
\def\yticks{\pgfkeysvalueof{/pgfplots/ytick}}

    \foreach \datafile in {50,500}
      {
        \addplot
          table
            [   mark=none,   skip first n = 2 ,
              x expr       = \thisrowno{0} + 0.412,
              y expr       = 
                ( 1000000 * \thisrowno{1} )
                  / sqrt ( \datafile  / 1000 )
            ]
          from {\datafile.ocw}; 
        \addlegendentryexpanded{\SI{\datafile}{\milli\volt\per\second}};
      };  
  \end{axis}
\end{tikzpicture}
\end{document}