グラフ上の凡例の位置を変更する

グラフ上の凡例の位置を変更する

subfigure外観を制御するために (1 ページに 2 列)を使用して、ドキュメントに活性化関数をプロットしたいと思います。

プロットに凡例を追加しましたが、この凡例がプロットの一部を覆っています。凡例の位置を左上に変更したいと思います。

これはその MWE です。

\documentclass[11pt]{article}

\usepackage{subfigure}
\usepackage{pgfplots}
\usepackage[top=3cm,left=3cm,right=3cm,bottom=3cm]{geometry}
% Scriptsize axis style.
\pgfplotsset{every axis/.append style={tick label style={/pgf/number format/fixed},font=\scriptsize,ylabel near ticks,xlabel near ticks,grid=major}}

\pgfplotsset{compat=1.16}

\begin{document}
\begin{figure}[t!]
    \centering
    \subfigure[Logistic sigmoid activation function.]{
            \begin{tikzpicture}[declare function={sigma(\x)=1/(1+exp(-\x));
            sigmap(\x)=sigma(\x)*(1-sigma(\x));}]
            \begin{axis}[width=5.5cm,height=4cm,ylabel=$\sigma(z)$,xlabel=$z$,ymin=0,ymax=1.25,xmin=-5,xmax=5]
                \addplot[blue,smooth] {1/(1+exp(-x))};
                \addplot[red,dotted,mark=none]   (x,{sigmap(x)});
                \legend{$\sigma(x)$,$\sigma'(x)$}
                %\addlegendentry{Logistic sigmoid}
            \end{axis}
        \end{tikzpicture}
    }
    \subfigure[Hyperbolic tangent activation function.]{
        \begin{tikzpicture}
            \begin{axis}[width=5.5cm,height=4cm,ylabel=$\tanh(z)$,xlabel=$z$,ymin=-1.25,ymax=1.25,xmin=-5,xmax=5]
                \addplot[blue,smooth] {tanh(x)};
                %\addlegendentry{Hyperbolic tangent}
            \end{axis}
        \end{tikzpicture}
    }
        \caption[Sigmoidal activation functions.]{Describe all functions here.}
        \label{fig:sigmoid-tanh}
\end{figure}
\end{document}

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

答え1

凡例のスタイルはキーで制御できますlegend style。たとえば、

legend style={at={(0.05,0.95)},anchor=north west}

左上隅に配置します。

\documentclass[11pt]{article}

\usepackage{subfigure}
\usepackage{pgfplots}
\usepackage[top=3cm,left=3cm,right=3cm,bottom=3cm]{geometry}
% Scriptsize axis style.
\pgfplotsset{every axis/.append style={tick label style={/pgf/number format/fixed},font=\scriptsize,ylabel near ticks,xlabel near ticks,grid=major}}

\pgfplotsset{compat=1.16}

\begin{document}
\begin{figure}[t!]
    \centering
    \subfigure[Logistic sigmoid activation function.]{
            \begin{tikzpicture}[declare function={sigma(\x)=1/(1+exp(-\x));
            sigmap(\x)=sigma(\x)*(1-sigma(\x));}]
            \begin{axis}[width=5.5cm,height=4cm,ylabel=$\sigma(z)$,xlabel=$z$,ymin=0,ymax=1.25,
            xmin=-5,xmax=5,legend style={at={(0.05,0.95)},anchor=north west}]
                \addplot[blue,smooth] {1/(1+exp(-x))};
                \addplot[red,dotted,mark=none]   (x,{sigmap(x)});
                \legend{$\sigma(x)$,$\sigma'(x)$}
                %\addlegendentry{Logistic sigmoid}
            \end{axis}
        \end{tikzpicture}
    }
    \subfigure[Hyperbolic tangent activation function.]{
        \begin{tikzpicture}
            \begin{axis}[width=5.5cm,height=4cm,ylabel=$\tanh(z)$,xlabel=$z$,
            ymin=-1.25,ymax=1.25,xmin=-5,xmax=5]
                \addplot[blue,smooth] {tanh(x)};
                %\addlegendentry{Hyperbolic tangent}
            \end{axis}
        \end{tikzpicture}
    }
        \caption[Sigmoidal activation functions.]{Describe all functions here.}
        \label{fig:sigmoid-tanh}
\end{figure}
\end{document}

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

ちなみに、subfigure非推奨と言われているので、subcaption代わりにパッケージを使用することをお勧めします。

\documentclass[11pt]{article}

\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage[top=3cm,left=3cm,right=3cm,bottom=3cm]{geometry}
% Scriptsize axis style.
\pgfplotsset{every axis/.append style={tick label style={/pgf/number format/fixed},font=\scriptsize,ylabel near ticks,xlabel near ticks,grid=major}}

\pgfplotsset{compat=1.16}

\begin{document}
\begin{figure}[t!]
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
       \centering
            \begin{tikzpicture}[declare function={sigma(\x)=1/(1+exp(-\x));
            sigmap(\x)=sigma(\x)*(1-sigma(\x));}]
            \begin{axis}[width=5.5cm,height=4cm,ylabel=$\sigma(z)$,xlabel=$z$,ymin=0,ymax=1.25,
            xmin=-5,xmax=5,legend style={at={(0.05,0.95)},anchor=north west}]
                \addplot[blue,smooth] {1/(1+exp(-x))};
                \addplot[red,dotted,mark=none]   (x,{sigmap(x)});
                \legend{$\sigma(x)$,$\sigma'(x)$}
                %\addlegendentry{Logistic sigmoid}
            \end{axis}
        \end{tikzpicture}
      \caption{Logistic sigmoid activation function.}   
    \end{subfigure}
    \quad
    \begin{subfigure}[b]{0.45\textwidth}
       \centering
         \begin{tikzpicture}
             \begin{axis}[width=5.5cm,height=4cm,ylabel=$\tanh(z)$,xlabel=$z$,
             ymin=-1.25,ymax=1.25,xmin=-5,xmax=5]
                 \addplot[blue,smooth] {tanh(x)};
                 %\addlegendentry{Hyperbolic tangent}
             \end{axis}
         \end{tikzpicture}
       \caption{Hyperbolic tangent activation function.}
    \end{subfigure}
        \caption[Sigmoidal activation functions.]{Describe all functions here.}
        \label{fig:sigmoid-tanh}
\end{figure}
\end{document}

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

関連情報