[Tex/LaTex] Customize length of y major grids in pgfplots

pgfplotstikz-pgftufte

Let's consider the example that can be found here:

http://pgfplots.net/tikz/examples/cyclic-voltammetry/ (There you can find a MWE and the data for it)

I would like to add some horizontals lines, as many as the ticks on the y axis and aligned with them, with the same length of the line of the x-axis (i.e., pretty much reimplement ymajorgrids, that in this case does not work, because the lines drew start from the y-axis to the end of the plot).

I know that the job can be done with \draw, but I don't understand how to iterate automatically through the y-ticks.

In addition, as a bonus question, I would like to know what is the default color used by pgfplots to plot ymajorgrids.

MWE

Code for completeness:

% This is a 'standalone' plot, so uses the standalone class
\documentclass{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{compat = newest}

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

\pgfplotsset{
  cyclic voltammetry/.style =
    {
      cycle list name = color list , 
      every x tick label/.append style  =
        { 
          /pgf/number format/.cd ,
           precision = 1 , 
           fixed         ,
           zerofill
        },
      xlabel = $E / \si{\volt} \textrm{ \emph{versus} } \ch{Fc+}/\ch{Fc}$,
      ylabel =
        $
          ( i / \si{\micro\ampere} )
            / \sqrt{\nu / ( \si{\milli\volt\per\second} ) }
        $,
    },
}

\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
    }
}
\makeatother

\begin{document}
\begin{tikzpicture}
  \begin{axis}%
    [
      tufte axes,

      every axis legend/.append style = {at = {(0.9,0.5)}}
    ]

    \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 )
            ]
          from {\datafile.ocw}; 
        \addlegendentryexpanded{\SI{\datafile}{\milli\volt\per\second}};
      };  
  \end{axis}
\end{tikzpicture}
\end{document}

Best Answer

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}