# PGFPlots: Centering axis lines with outside tick labels

What I am trying to achieve:

Basically I want to get as close to this picture as possible:

Note that the axis lines are centered but that the tick labels are on the outside (similar to the boxed axis style).

What I have so far:

\documentclass{minimal}

\usepackage{pgfplots}
\usepackage{graphicx}   % For \resizebox

\begin{document}
\centering
\resizebox{0.8\textwidth}{!}{%
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
grid=major,
xlabel=$$x$$, xmin=-5.25, xmax=5.25,
xtick distance=1, xtick pos=bottom,
ymin=-5.25, ymax=5.25,
ytick distance=1, ytick pos=left,
]

\end{axis}
\end{tikzpicture}
} % \resizebox
\end{document}


As you can see the ticks themselves were indeed moved to left and bottom as specified by xticklabel pos=bottom and yticklabel pos=left.

Note that, according to the PGFPlots manual Revision 1.16, section 4.15.2 (Tick Alignment: Positions and Shifts):

Changing tick pos will also affect the placement of tick labels.

However, this does not seem to be the case here.

Here I show one possible solution to achieve what you want.

The quoted text in your question is maybe a bit misleading, but it is also written there, that this only affects boxed plots.

Nevertheless, have a look at the comments in the code to get further information.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-5.25,
xmax=5.25,
ymin=-5.25,
ymax=5.25,
xlabel=$x$,
% move the xlabel to the right of the arrow
xlabel style={
anchor=west,
},
xtick distance=1,
xtick pos=bottom,
ytick distance=1,
ytick pos=left,
grid=major,
% -----------------------------
% move ticklabels to the "outside"
xticklabel style={
%            % hard-coded solution --> dependent of axis scaling/size
%            yshift=-82pt,
% relative shifting --> independent of axis scaling/size
shift={(0,0 |- {axis description cs:0,-1})}
},
yticklabel style={
%            % hard-coded solution --> dependent of axis scaling/size
%            xshift=-100pt,
% relative shifting --> independent of axis scaling/size
shift={(0,0 -| {axis description cs:-0.53,0})}
},
% remove the ticks themselves
% (which are moved as well)
tickwidth=0pt,
% show the "0"s (zeros) again
hide obscured x ticks=false,
hide obscured y ticks=false,
% -----------------------------
%        % for testing only to see, if the shifting works independent of
%        % axis scaling
%        height=10cm,
]

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


• Thank you, this looks very good. I am however surprised there is no pre-defined option in PGFPlots to do this. – Leon Feb 20 at 16:01
• Nice answer! I believe you can improve it by not hard-coding the shifts, e.g., xticklabel style={shift={(0,0 |- current axis.south of origin)}}. – Emma Feb 20 at 17:15
• @Emma, that was a great idea but unfortunately "outer" anchors cannot be used inside the axis environment, because at that time they are not defined. But the axis description cs is and thus, one can use this to relatively shift the ticklabels and thus the solution works independent of the axis size/scaling. Many thanks for that hint! I adapted my answer accordingly. – Stefan Pinnow Feb 20 at 18:55
• @StefanPinnow Huh, for some reason using current axis got the desired result for me, even though it compiled with errors (which I didn't notice). Using axis description cs is clearly better. Better still I think would be to use xticklabel* cs for the y tick labels and yticklabel* cs for the x labels, to avoid the pesky -0.53. – Emma Feb 20 at 21:10