如何移動 pgfplot 中的 y 軸標籤,使其不妨礙軸?

如何移動 pgfplot 中的 y 軸標籤,使其不妨礙軸?

我正在嘗試使用 pgfplots 繪製以下圖表:

\begin{equation*}
\begin{tikzpicture}
    \begin{axis}[
    axis lines=middle,
    axis line style={latex-latex},
    xmin=-5,ymin=-1,xmax=5,ymax=2,
    samples=100,
    grid=major,
    xlabel={\(x\)},
    ylabel={\(ReLU(x) = max(0,x)\)},
    title={Rectified Linear Unit Function}]
\addplot[black, thick]{max(0,x)};
\end{axis}
\end{tikzpicture}
\end{equation*}

看起來像這樣:

在此輸入影像描述

有什麼方法可以將 y 軸標籤移動到軸的左側,這樣它就不會遮擋圖形嗎?我知道我可以將它放在軸的末端,但它會遮住標題。

答案1

歡迎!您可以使用ylabel按鍵控制 的外觀ylabel style

\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\re}{Re}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{equation*}
\begin{tikzpicture}
    \begin{axis}[
    axis lines=middle,
    axis line style={latex-latex},
    xmin=-5,ymin=-1,xmax=5,ymax=2,
    samples=100,
    grid=major,
    xlabel={\(x\)},
    ylabel={\(\re LU(x) = \max(0,x)\)},
    ylabel style={yshift=-0.5ex,anchor=north east},
    title={Rectified Linear Unit Function}]
\addplot[black, thick,samples at={-5,0,5}]{max(0,x)};
\end{axis}
\end{tikzpicture}
\end{equation*}
\end{document}

在此輸入影像描述

我不知道將繪圖放置在equation*環境中是否是個好主意,但我暫時保留了它,但我確實認為使用\max\re作為相應的數學運算符是個好主意。我還用它samples at={-5,0,5}來更公正地執行該max功能。

不過,您可能需要刪除 處的勾號標籤y=2

\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\re}{Re}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    axis lines=middle,
    axis line style={latex-latex},
    xmin=-5,ymin=-1,xmax=5,ymax=2,
    ytick={1},
    samples=100,
    grid=major,
    xlabel={\(x\)},
    ylabel={\(\re LU(x) = \max(0,x)\)},
    ylabel style={yshift=-0.5ex,anchor=east},
    title={Rectified Linear Unit Function}]
\addplot[black, thick,samples at={-5,0,5}]{max(0,x)};
\end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容