0

I'm having trouble with my tikzpicture. I have the following code:

          \begin{center}
                    \begin{tikzpicture}[scale = 1]
                    \begin{axis}[
                          axis x line=center,
                          axis y line=center,
                          legend entries={Creciente, Estrictamente Creciente},
                          legend pos=north,
                          xtick={-5,-4,...,5},
                          ytick={-5,-4,...,5},
                          xlabel={$x$},
                          ylabel={$y$},
                          xlabel style={below right},
                          ylabel style={above left},
                          xmin=-5.5,
                          xmax=5.5,
                          ymin=-5.5,
                          ymax=5.5]
                        \addlegendimage{no markers,magenta}
                        \addlegendimage{no markers,blue}
                    \addplot [
                        domain=-5:1, 
                        samples=100, 
                        color=magenta]
                        {x/2};
                    \addplot [
                        domain=1:2, 
                        samples=100, 
                        color=magenta]
                        {0.5};
                    \addplot [
                        domain=2:5, 
                        samples=100, 
                        color=magenta]
                        {(x/2)-0.5};
                    \addplot [
                        domain=-5:5, 
                        samples=100, 
                        color=blue]
                        {(x/2)+0.5};
                \end{axis}

            \end{tikzpicture}
        \end{center}

The image looks ok but an error appears in the log: Package pgfkeys Error: Choice 'center' unknown in choice key '/pgfplots/legend pos'. I am going to ignore this key. I think that the problem are the legend entries but I can't figure out the error.

1 Answers1

3

The problem you face is that north is not a valid value for legend pos. According to the manual (section 4.9. on "Axis Descriptions") you can only use south west, south east, north west, north east or outer north east.

However, the manual also states that using this sytax essentially is a shorthand for setting the options at and anchor. The default value for at according to the manual is (0.98,0.98) which denotes north east. The values for at describe the position on the x and y axes respectively, so that 0 denotes the lower left and 1 the upper right corner of the plot.

So, you can place the legend at the north anchor of the plot using legend style={at={(0.5,0.98)}, anchor=north}. (Note that anchor=north here describes the anchor of the legend, not the anchor of the plot.)

Of course, placing the legend there would have it covering the y axis.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}[scale = 1] \begin{axis}[ axis x line=center, axis y line=center, legend entries={Creciente, Estrictamente Creciente}, legend style={at={(0.5,0.98)}, anchor=north}, xtick={-5,-4,...,5}, ytick={-5,-4,...,5}, xlabel={$x$}, ylabel={$y$}, xlabel style={below right}, ylabel style={above left}, xmin=-5.5, xmax=5.5, ymin=-5.5, ymax=5.5] \addlegendimage{no markers,magenta} \addlegendimage{no markers,blue}

    \addplot [
        domain=-5:1, 
        samples=100, 
        color=magenta]
        {x/2};
    \addplot [
        domain=1:2, 
        samples=100, 
        color=magenta]
        {0.5};
    \addplot [
        domain=2:5, 
        samples=100, 
        color=magenta]
        {(x/2)-0.5};
    \addplot [
        domain=-5:5, 
        samples=100, 
        color=blue]
        {(x/2)+0.5};
\end{axis}

\end{tikzpicture}

\end{document}

enter image description here