Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

For adding zero lines to every plot produced by pgfplots, I used the code provided by percusse here. (A quick note after 2 replies were provided: this is a "global" solution. If one likes to add the zero lines to one plot only, see esdd's reply.)

The zero lines are the last item added to every axis environment so they'll get drawn over anything that was drawn before. This is a bit ugly in the case that a pin or node is next to/on the y-axis. Of course I can move some pins' positions by changing their parameters but all in all, some will have to stay near/on the y-axis so it isn't possible for all pins.

Is this possible to solve? Or would anyone have to offer some advice anyway?

Picture of Problem

enter image description here

MWE

\documentclass{article}
\usepackage{pgfplots}

\tikzset{
every pin/.style={
    draw=black,
    fill=white
},
every pin edge/.style={
    draw=black!60,
    fill=white,
    thick
},}

\pgfplotsset{
every axis/.append style={
%
xmajorgrids={true},
ymajorgrids={true},
xminorgrids={false},
yminorgrids={false},
%
after end axis/.code={
    %   \fill[red] (axis cs:0,0) circle(5pt);
    \draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
    \draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},
},
every tick/.append style={
    black,
    thick
},
/pgf/number format/.cd,
use comma,
1000 sep={.},
}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
    \addplot {rand};
    \filldraw (axis cs:0.9,0.9) circle [radius=1pt];
    \node[pin={[pin distance=0.3cm]180:{0.9} }] at (axis cs:0.9,0.9) {};
    \node[pin=0:{0.9}] at (axis cs:0.9,0.9) {};
    \end{axis}
    \end{tikzpicture}
\end{document}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can move the drawing as being the first thing to be drawn via execute at begin axis key. Hence the only modification required is

execute at begin axis={%
    %   \fill[red] (axis cs:0,0) circle(5pt);
    \draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
    \draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},

then you get,

enter image description here

share|improve this answer

Here is a suggestion using the special node current axis with the anchors above origin, below origin, left of origin and right of origin:

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{
  every pin/.style={draw=black,fill=white},
  every pin edge/.style={draw=black!60,fill=white,thick},
}

\pgfplotsset{
  every axis/.append style={
    xmajorgrids={true},
    ymajorgrids={true},
    xminorgrids={false},
    yminorgrids={false},
  },
  every tick/.append style={black,thick},
  /pgf/number format/.cd,
  use comma,
  1000 sep={.},
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
% zero lines:
    \draw[thin] (current axis.above origin) -- (current axis.below origin);
    \draw[thin] (current axis.left of origin) -- (current axis.right of origin);
%
    \addplot {rand};
    \filldraw (axis cs:0.9,0.9) circle [radius=1pt];
    \node[pin={[pin distance=0.3cm]180:{0.9} }] at (axis cs:0.9,0.9) {};
    \node[pin=0:{0.9}] at (axis cs:0.9,0.9) {};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Edit: Of course this node can also be used in execute at begin axis:

\pgfplotsset{
  every axis/.append style={
    xmajorgrids={true},
    ymajorgrids={true},
    xminorgrids={false},
    yminorgrids={false},clip=false,
  },
  execute at begin axis={%
    \draw[thin] (current axis.above origin) -- (current axis.below origin);
    \draw[thin] (current axis.left of origin) -- (current axis.right of origin);
  },
  every tick/.append style={black,thick},
  /pgf/number format/.cd,
  use comma,
  1000 sep={.},
}
share|improve this answer
    
Not what I was looking for at all and I already know about it, but definitely something others might find interesting! I linked your post in the op. –  henry Jun 7 at 13:55
1  
You can use this node also in execute at begin axis. –  esdd Jun 7 at 14:14
    
I see, thanks for clarifying. :) –  henry Jun 7 at 14:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.