軸ラベルの絶対座標と相対座標を組み合わせる

軸ラベルの絶対座標と相対座標を組み合わせる

次の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}

生産された出力

これはまさに私が望んでいたものです。問題は、stdaxis高さの異なる別の画像に同じスタイルを使用すると、y軸ラベル「f(x)」が高くなりすぎたり低すぎたりすることです。これを回避するには、絶対位置を使用して、スタイルを次のように変更します。ylabel style={at={(ticklabel cs:1.0+5mm)},anchor=west,rotate=270}

どうしてこんなことが可能なのでしょうか?

答え1

これは、TikZ ライブラリを使用した座標計算で実現できますcalc(cf.PGF/TikZマニュアルのセクション13.5):

\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}

プロット例。y ラベルは軸ボックスの左上隅から 5mm 上にあります。

答え2

使用yshift=5mm rotate=270:

ここに画像の説明を入力してください

コード:

\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}

関連情報