使用图例条目时出现未知错误

使用图例条目时出现未知错误

我的 tikzpicture 出了问题。我有以下代码:

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

图像看起来不错,但日志中出现错误:软件包 pgfkeys 错误:选择键 '/pgfplots/legend pos' 中的选择 'center' 未知。我将忽略此键。我认为问题出在图例条目上,但我无法找出错误。

答案1

您面临的问题是north不是 的有效值legend pos。根据手动的(第 4.9 节“轴描述”) ,您只能使用south westsouth eastnorth west或。north eastouter north east

但是,手册还指出,使用此语法本质上是设置选项at和 的简写。根据手册, 的anchor默认值为 ,表示。 的值分别描述 x 轴和 y 轴上的位置,因此 0 表示图的左下角,1 表示图的右上角。at(0.98,0.98)north eastat

因此,您可以使用 将图例放置在north绘图的锚点处legend style={at={(0.5,0.98)}, anchor=north}。 (请注意,anchor=north这里描述的是图例的锚点,而不是绘图的锚点。)

当然,将图例放在那里会覆盖 y 轴。

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

在此处输入图片描述

相关内容