7

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}
Stefan Pinnow
  • 29,535
Pierpaolo
  • 786

2 Answers2

5

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

Stefan Pinnow
  • 29,535
3

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}
anderstood
  • 2,196
  • 19
  • 35