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{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.
\yticks
\pgfplotsinvokeforeach
is used instead offoreach
because of theaxis
environment (see for example this question)\draw
using extremal x-coordinates from data.Complete code: