# Customize length of y major grids in pgfplots

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.

Code for completeness:

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

\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}
{
table
[

skip first n = 2 ,

x expr       = \thisrowno{0} + 0.412,

y expr       =
( 1000000 * \thisrowno{1} )
/ sqrt ( \datafile  / 1000 )
]
from {\datafile.ocw};
};
\end{axis}
\end{tikzpicture}
\end{document}


This does the trick.

• 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{
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}
{
table
[   mark=none,   skip first n = 2 ,
x expr       = \thisrowno{0} + 0.412,
y expr       =
( 1000000 * \thisrowno{1} )
/ sqrt ( \datafile  / 1000 )
]
from {\datafile.ocw};
};
\end{axis}
\end{tikzpicture}
\end{document}

• Any way to get rid of the necessity of specifying manually the ticks? – Pierpaolo Jun 10 '15 at 11:46
• @thunder1123 Of course, see Edit. – anderstood Jun 10 '15 at 18:52
• Coming back to this answer, the method of automatic accessing the ticks doesn't work. (Try to comment out \def\yticks{-100,-50,0}). Do you have any idea on how to overcome it ? – Pierpaolo Sep 22 '15 at 4:48

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} {