This is a follow-up question to Setting ticklabels in pgfplot
The solution Stefan posted works very good.
But when I use y dir=reverse the secondlast label is therefore near the origin (see example below).
I tried to change \lastyticknum to \firstyticknum, but this gives me the same result. Any ideas?
\documentclass[a4paper]{article}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.5,
% adjusted from <https://tex.stackexchange.com/a/416810/95441>
din yunit2/.style={
yticklabel style={
name=ylabel\ticknum,
append after command=\pgfextra{\xdef\firstyticknum{\ticknum}}
},
% changed `.code' to `.append code'
after end axis/.append code={
\pgfmathparse{int(\firstyticknum-1)}
% first "overdraw" the node by rectangle, so it isn't seen any more
\fill [white]
(ylabel\pgfmathresult.south west)
rectangle
% the `xshift' is needed to not overdraw the axis line
([xshift=-0.4pt]ylabel\pgfmathresult.north east);
% then place the "unit" text
\node [anchor=base] at (ylabel\pgfmathresult.base) {#1};
},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xtick distance=1,
ytick distance=500,
din yunit2={\si{\meter}},
y dir=reverse,
ylabel={test},
legend pos={north west}
]
\addplot [no marks, blue] table [x=a, y=b] {
a b
1 370
2 -740
3 -1110
4 -1480
5 -1850
6 -2220
7 -2590
8 -2960
9 -3330
};
\legend{test}
\end{axis}
\end{tikzpicture}
\end{document}

