Scaling pgfplot axis with labels at end of axis

pgfplots

I have the following tikzpicture where the axis labels are anchored at the end of the axes. (code is at the bottom)

enter image description here

So far so good except that when I want to scale the axes (uncomment the line below), the labels goes off the supposed positions. In this example, it even fails when the x and y scales are set to the same value. (It works if I do scale=0.7 in this case, but in general different scale factors might also be needed for the two axes)

enter image description here

Any suggestion on how this can be fixed please?

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}


\pgfplotsset{
  compat=1.17,
    mystyle/.style={
        axis x line=middle,
        axis y line=middle,
        every axis x label/.style={at={(current axis.right of origin)},anchor=west},
        every axis y label/.style={at={(current axis.above origin)},anchor=south},
        every axis plot/.append style={mark = none, draw=black},
    },
}


\begin{document}

\begin{tikzpicture}
\begin{axis}[
       mystyle,
        xlabel=xlabel,
        ylabel=ylabel,
        % xscale=0.7, yscale=0.7  % uncomment this
  ]
  \addplot coordinates {(1,1) (2,-1)};
\end{axis}
\end{tikzpicture}




\end{document}

Best Answer

If you replace

every axis x label/.style={at={(current axis.right of origin)},anchor=west}

with

xlabel style={anchor=west},

then using xscale=0.7 works fine:

enter image description here

Notes:

  • Axis labels were modified to be in math mode.

  • Using ymax=1.1 eliminated the issue of the dot being on top of the arrow.

Code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{
  compat=1.17,
    mystyle/.style={
        axis x line=middle,
        axis y line=middle,
        %every axis x label/.style={at={(current axis.right of origin)},anchor=west},
        xlabel style={anchor=west},% <----- Revision
        every axis y label/.style={at={(current axis.above origin)},anchor=south},
        every axis plot/.append style={mark = none, draw=black},
    },
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
       mystyle,
        xlabel=$x$,% <----- Now in math mode
        ylabel=$y$,
        xscale=0.7, yscale=0.7,
        ymax=1.1,% <----- Added
  ]
  \addplot coordinates {(1,1) (2,-1)};
\end{axis}
\end{tikzpicture}
\end{document}