For pgfplots, in all the 2D examples I've seen so far, 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 do they go through some other user-specified coordinate pairs?

2 Answers 2


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

    axis lines=middle
\addplot {x^2+rand};

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.

    axis line origin/.style args={#1,#2}{
        x filter/.append code={ % Check for empty or filtered out numbers
        y filter/.append code={

    axis lines=middle,
    axis line origin={1,2}
\addplot +[domain=-2:2] {x^2+rand};


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

\begin{axis}[axis lines = middle]


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

and so on.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .