17

I want to place a zero in a plot with middle axis. How can I place is so that it is exactly on the same vertical position as the ytick labels and on the same horizontal position as the xtick labels?

    \documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[domain=-3:4,
 no markers,
 axis lines=middle,
 xtick distance=1,ytick distance=1,
 x=1cm,
 y=1cm,
 ymin=-2,
 extra x ticks=0,
 extra x tick labels=$0_x$,
 extra x tick style={
     tick label style={
     anchor=near xticklabel}},
 extra y ticks=0,
 extra y tick labels=$0_y$,]
\addplot {x};
\addplot {-x};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

4
  • I think the bonus question edit can be simply removed since it's not longer relevant. :P
    – Alenanno
    Commented Sep 12, 2016 at 10:19
  • @Alenanno: I will remove it in a few hours - in case someone saw the question and is still wondering why the label appeared at the wrong position. Commented Sep 12, 2016 at 10:57
  • Did you know about hide obscured x ticks or y ?
    – percusse
    Commented Sep 12, 2016 at 11:54
  • @percusse: "knowing" is saying to much -- obviously something in the code was hiding the default zero, and quite probably there would be an option. But as I didn't want the zero to appear at its normal position changing the option wouldn't help. Commented Sep 12, 2016 at 12:20

1 Answer 1

11

Assuming the tick length are the same for both the major x ticks and the major y ticks, you can use

\pgfkeysgetvalue{/pgfplots/major tick length}{\labelshift}
\node[
    anchor=north east,
    %draw,green% to show the border of the node
    ]
    at({xticklabel* cs:0,.5*\labelshift}-|{yticklabel* cs:0,.5*\labelshift})
    {\pgfmathprintnumber{0}};

or

\node[
    anchor=north east,
    outer sep=.5*\pgflinewidth+.5*\pgfkeysvalueof{/pgfplots/major tick length},
    %draw,green% to show the border of the node
    ]
    at({xticklabel* cs:0}-|{yticklabel* cs:0})
    {\pgfmathprintnumber{0}};

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    domain=-3:4,
    no markers,
    axis lines=middle,
    xtick distance=1,ytick distance=1,
    x=1cm,
    y=1cm,
    ymin=-2,
    extra x ticks=0,
    extra x tick labels=$0_x$,
    extra y ticks=0,
    extra y tick labels=$0_y$,
    ticklabel style={draw=gray}% to show the border of the nodes
]
\addplot {x};
\addplot {-x};

\pgfkeysgetvalue{/pgfplots/major tick length}{\labelshift}
\node[
    anchor=north east,
    draw,green% to show the border of the node
    ]
    at({xticklabel* cs:0,.5*\labelshift}-|{yticklabel* cs:0,.5*\labelshift})
    {\pgfmathprintnumber{0}};
%% or:
%\node[
    %anchor=north east,
    %outer sep=.5*\pgflinewidth+.5*\pgfkeysvalueof{/pgfplots/major tick length},
    %draw,green% to show the border of the node
    %]
    %at({xticklabel* cs:0}-|{yticklabel* cs:0})
    %{\pgfmathprintnumber{0}};

\end{axis}
\end{tikzpicture}
\end{document}

If there are no other extra x ticks you can also use

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    domain=-3:4,
    no markers,
    axis lines=middle,
    xtick distance=1,ytick distance=1,
    x=1cm,
    y=1cm,
    ymin=-2,
    extra x ticks=0,
    extra x tick style={
        tick label style={
            anchor=north east,
            xshift=-.5*\pgfkeysvalueof{/pgfplots/major tick length},
            green% to color this node
    }},
    extra y ticks=0,
    extra y tick labels=$0_y$,
    ticklabel style={draw=gray}% to show the border of the nodes
]
\addplot {x};
\addplot {-x};
\end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

1
  • Perfect, the \pgfkeysvalueof{/pgfplots/major tick length} was the missing trick ... Commented Sep 12, 2016 at 12:18

You must log in to answer this question.

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