2

I am currently trying to relocate the legend of my plot at the top left corner. But as soon as I set the command [legend pos=north west], the legend is relocated, but the grid lines are not rendered anymore and the axis range is completely off.

error?

\documentclass[11pt]{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=2]
    \begin{axis}[legend pos=north west]
    [
        grid=major,
        xmin=-5,
        xmax=3,
        ymin=-2,
        xmax=3,
        axis x line=middle,
        ytick={-1, 0, 1},
        ymax=2,
        axis y line=middle,
        legend cell align={left}
    ]
        \addplot%0.1
        [
            Dandelion,%
            line width=2pt,
            mark=none,
            samples=100,
            domain=-10:10,
        ]
        (x,{x*(1/(1+exp(-0.1*x)))});     \addlegendentry{$\beta = 0.1$}

\end{axis}
\end{tikzpicture}

\end{document}
  • 1
    Sorry! I edited the MWE – slandolt Mar 4 at 4:02
1

The main issue is that you have closing ] after legend pos=north west, so the other options get ignored. As Joule V points out, this key doesn't yield the desired position. The legend can be placed with legend style={at={(axis cs:0,2)},....

\documentclass[11pt]{article}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}[scale=2]
    \begin{axis}[grid=major,
        xmin=-5,
        xmax=3,
        ymin=-2,
        xmax=3,
        %axis x line=middle,
        ytick={-1, 0, 1},
        ymax=2,
         legend style={at={(axis cs:0,2)},
        anchor=north west,outer sep=3pt},
        %axis y line=middle,
        legend cell align={left},
        axis lines=middle,
    ]
        \addplot%0.1
        [
            Dandelion,%
            line width=2pt,
            mark=none,
            samples=100,
            domain=-5:3,
        ]
        (x,{x*(1/(1+exp(-0.1*x)))});     
    \addlegendentry{$\beta = 0.1$}

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.