3

How to position the second label above the second axis?

In the MWE below, simply declaring \pgfplotsset{compat=newest} solves the problem. However, I would like to control the label position locally in a document where the compat bounds cannot be set (think of it as temporary workaround). Is there a way to explicitly specify the label's position inside the second axis?

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ymax=1,
        axis y line*=left,
        ylabel={left},
    ]
    \addplot [color=blue, domain=0:1] expression {x} node[midway]{axis 1};
    \end{axis}
    \begin{axis}[
        ymin=0,
        ymax=2,
        axis x line=none,
        axis y line*=right,
        ylabel={\hspace{4cm} should be right},
    ]
    \addplot [color=red, domain=0:1] expression {x} node[midway]{axis 2};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    Try adding the ylabel near ticks option to your second axis.
    – erik
    Commented Dec 14, 2021 at 15:32

3 Answers 3

3

Without specifying compat inside a pgfplotsset command, adding ylabel near ticks should produce what you want. However, the documentation (section 4.9.3) mentions that using pgfplotsset is preferred to make this behavior consistent document-wide and to avoid any unnecessary space (as fixed distances are used if activated outside of the preamble).

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ymax=1,
        axis y line*=left,
        ylabel={left},
    ]
    \addplot [color=blue, domain=0:1] expression {x} node[midway]{axis 1};
    \end{axis}
    \begin{axis}[
        ymin=0,
        ymax=2,
        axis x line=none,
        axis y line*=right,
        ylabel near ticks,
        ylabel={should be right},
    ]
    \addplot [color=red, domain=0:1] expression {x} node[midway]{axis 2};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • That's what I was looking for, thanks erik!
    – PatrickT
    Commented Dec 14, 2021 at 16:20
3

You should alway set compat level. I do not understand what "more complicated document" can not set this level. If you want to manually control the position of the label, it can be done like this:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0,
ymax=1,
axis y line*=left,
ylabel={left},
]
\addplot [color=blue, domain=0:1] expression {x} node[midway]{axis 1};
\end{axis}
\begin{axis}[
ymin=0,
ymax=2,
axis x line=none,
axis y line*=right,
ylabel={should be right},
y label style={at={(1.1,0.5)}}
]
\addplot [color=red, domain=0:1] expression {x} node[midway]{axis 2};
\end{axis}
\end{tikzpicture}
\end{document}

Graph with two axis and labels

1
  • Thanks hpekristiansen, y label style={at={(1.1,0.5)}} works too. I picked Erik's answer simply because it came earlier. The "more complicated" document was not clear, sorry: basically I had settings I wanted to override locally.
    – PatrickT
    Commented Dec 14, 2021 at 16:33
1

If you just add \pgfplotsset{compat=1.18} it looks like you want.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ymax=1,
        axis y line*=left,
        ylabel={left},
    ]
    \addplot [color=blue, domain=0:1] expression {x} node[midway]{axis 1};
    \end{axis}
    \begin{axis}[
        ymin=0,
        ymax=2,
        axis x line=none,
        axis y line*=right,
        ylabel={right},
    ]
    \addplot [color=red, domain=0:1] expression {x} node[midway]{axis 2};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • Is that not just what OP writes? Commented Dec 14, 2021 at 15:11
  • Thanks John, I was looking for a way to align without setting compat.
    – PatrickT
    Commented Dec 14, 2021 at 16:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .