4

Consider the following MWE

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.12,
    stdaxis/.style={
        ylabel style={at={(ticklabel cs:1.06)},anchor=west,rotate=270},
        }
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[stdaxis,ylabel=$f(x)$,xlabel=$x$]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

produced output

This is exactly what I want. My problem is that when I use the same stdaxis style for another picture with a different height, the y-axis label "f(x)" gets too high or too low. To circumvent this, I would like to use an absolute positioning and change the style to something like ylabel style={at={(ticklabel cs:1.0+5mm)},anchor=west,rotate=270}

How is this possible?

3

You can achieve this with coordinate calculations using the TikZ library calc (cf. section 13.5 of the PGF/TikZ manual):

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{
    compat=1.12,
    stdaxis/.style={
        ylabel style={at={($(ticklabel cs:1.0)+(0mm,5mm)$)},anchor=west,rotate=270},
        }
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[stdaxis,ylabel=$f(x)$,xlabel=$x$]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

example plot, with y label 5mm above the upper left corner of the axis box

  • I almost tried this, but my mistake was that I assumed, the second coordinate should be one-dimensional, too (as is the axis coordinate). Thanks! Note: esdd's answer produces exactly the same output – riddleculous Mar 27 '16 at 0:20
4

Use yshift=5mm after rotate=270:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.12,
    stdaxis/.style={
            ylabel style={at={(ticklabel cs:1)},
            anchor=west,
            rotate=270,
            yshift=5mm% <-
            },
        }
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[stdaxis,ylabel=$f(x)$,xlabel=$x$]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}

\bigskip
\begin{tikzpicture}
    \begin{axis}[stdaxis,height=10cm,ylabel=$f(x)$,xlabel=$x$]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}
  • thanks for clarifying the yshift position! So far I didn't the logic behind this yet, I tried similar things but not after the rotate. As diabonas' anser was a bit faster and seems better understandable to me, I accepted that one, although yours produces exactly the same result and does not even need a further library loaded – riddleculous Mar 27 '16 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.