# pgfplots: how make axes go through origin?

For pgfplots, in all the 2D examples I've seen, and in the manual, the location of axes seems to be specified in terms of location within the plot box, i.e.,relative to top, bottom. How does one specify that x- and y-axes intersect at the origin? And how does that they go through some other user-specified coordinate pair?

As Joseph said, to get the axis lines to pass through the origin, you can use the axis lines=middle option:

\begin{axis}[
axis lines=middle
]
\end{axis}


To choose an arbitrary point for the axis lines to pass through, a pragmatic approach might be to transform the data points and the labels using the x filter/.code and xticklabel keys, which can be wrapped in a custom style like axis lines origin={<x>,<y>}. The problem with this approach is that the tick labels can end up quite weird depending on the origin you choose.

\pgfplotsset{
axis line origin/.style args={#1,#2}{
x filter/.append code={ % Check for empty or filtered out numbers
\ifx\pgfmathresult\empty\else\pgfmathparse{\pgfmathresult-#1}\fi
},
y filter/.append code={
\ifx\pgfmathresult\empty\else\pgfmathparse{\pgfmathresult-#2}\fi
},
xticklabel=\pgfmathparse{\tick+#1}\pgfmathprintnumber{\pgfmathresult},
yticklabel=\pgfmathparse{\tick+#2}\pgfmathprintnumber{\pgfmathresult}
}
}
...

\begin{axis}[
axis lines=middle,
axis line origin={1,2}
]
\end{axis}


To plot an axis through the origin, you want the slightly misleading middle position:

\begin{axis}[axis lines = middle]


or

\begin{axis}[axis x line = middle]


and so on.