1

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}

enter image description here

sporc
  • 643

1 Answers1

2

On a reversed axis it is clear that you want to add the label at the second tick, thus you simply need to replace ylabel-\pgfmathresult by ylabel-2 to make it work. (Please note that I added a dash in the ticklabel names.)

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.5,
        % adjusted from <https://tex.stackexchange.com/a/416810/95441>
        din yunit reverse/.style={
            yticklabel style={
                name=ylabel-\ticknum,
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                % first "overdraw" the node by rectangle, so it isn't seen any more
                \fill [white]
                    (ylabel-2.south west)
                        rectangle
                    % the `xshift' is needed to not overdraw the axis line
                    ([xshift=-0.4pt]ylabel-2.north east);
                % then place the "unit" text
                \node [anchor=base] at (ylabel-2.base) {#1};
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick distance=1,
        ytick distance=500,
        din yunit reverse={\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}

image showing the result of above code

Stefan Pinnow
  • 29,535