# pgfplots: How to add the origin to the axis in a robust way?

## Question

I add the origin to my graph with

after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}


(code found here). This works fine is the origin is on my axis. But, there are problems if 0 is not in the values taken by y (see the pictures below).

Is there a way to fix this issue?

By this, I mean an option that would display the origin only when needed.

## Pictures

### second picture

I cannot put it here, but it prints two pages!!!

## Code

### first code

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 2:5,
xmin = 2,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
restrict y to domain=1:2,
ymin = 1,
ymax = 2,
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}


### second code

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 2:5,
xmin = 2,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
%restrict y to domain=1:2,
%ymin = 1,
%ymax = 2,
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}

-
You most likely want to use rel axis cs instead of axis cs to set the origin. – Henri Menke Apr 8 '14 at 17:26
You can use the axis description coordinate system instead: after end axis/.code={ \node[left=2pt] at (axis description cs:0,0) {0}; \node[below=2pt] at (axis description cs:0,0) {0}; } – Gonzalo Medina Apr 8 '14 at 17:29
Hi! Both solution work and don't work, in a sense. The origin is displayed in a good way, but since 0 is out the range, the origin in this case should not be displayed. – Colas Apr 8 '14 at 17:33
Some remarks: I don't know why (and what is the loss in this trick), but the behavior is better with before than with after. More precisely, before end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0} ;} is better. – Colas Apr 8 '14 at 18:06
@Colas: Probably better due to the fact that with before end axis you are still within the axis environment so that pgf can determine if that text is within the bounds of the plot, but after end axis pgf is unable to make that determination. – Peter Grill Apr 8 '14 at 18:12

You can use axis direction cs as in

\path (axis direction cs:0,0) node [anchor=north west,yshift=-0.65cm,xshift=-0.7cm] {0} node [anchor=south east,xshift=-0.5cm,yshift=-0.7cm] {0};}]


Code:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 2:5,
xmin = 2,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
restrict y to domain=1:2,
ymin = 1,
ymax = 2,
after end axis/.code={\path (axis direction cs:0,0) node [anchor=north west,yshift=-0.65cm,xshift=-0.7cm] {0} node [anchor=south east,xshift=-0.5cm,yshift=-0.7cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}


there is also axis description cs which you may like to exploit for this purpose.

after end axis/.code={\path (axis description cs:0,0) node [anchor=north west,yshift=-0.075cm,xshift=-0.2cm] {0} node [anchor=south east,xshift=-0.075cm,yshift=-0.2cm] {0};}]


However for this particular graph, (0,0) position is completely wrong as seen in the picture.

-