在 pgfplots 中如何设置图例的长度

在 pgfplots 中如何设置图例的长度

我使用 pgfplots 在 LaTeX 中制作了一个图表,但是当我将方程组纳入图例时,图例长度变得太小。而且我似乎无法增加图例长度。

我的代码:

\documentclass[a4paper,11pt,titlepage]{report}

\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}

\pgfplotsset{compat=1.12}
\usetikzlibrary{patterns, decorations.markings, arrows}

\begin{document}

\begin{tikzpicture}
\begin{axis}
    [
    scale=1.25,
    xmin = -2,  xmax=2,
    ymin = -1,  ymax=8,
    xlabel = $x_1$,
    ylabel = $f(x_1)$,
    grid = both,
    grid style ={line width=.1pt, draw=darkgray!10},
    major grid style ={line width=.2pt, draw=darkgray!50},
    axis line style = {->},
    axis lines = middle,
    minor tick num = 4,
    enlargelimits={abs=0.5},
    axis line style = {latex-latex},
    samples = 100,
    domain = -3:3,
    legend pos = outer north east,
    legend style = {nodes={align=left,anchor=west,scale=0.7}},
    ]
    % Nonlinear function
    \addplot[draw=red, thick] {x^2+2};
    \addlegendentry{$f(x_1) = x^2+2$}

    % f(x_1) <= 4
    \addplot[draw=Aquamarine, thick, opacity=0.7, postaction={
        decorate, decoration={markings, 
        mark = at position 4/24 with {\arrow[Aquamarine, rotate=-90]{latex}},
        mark = at position 5/6 with {\arrow[Aquamarine, rotate=-90]{latex}},
                }}] coordinates{(-3, 4) (3,4)};
    \addlegendentry{$f(x_1) \leq 4$}

    % x_1 >= -1
    \addplot[draw=Aquamarine, thick, opacity=0.7, legend image post style={rotate=90, xshift=-0.3cm, yshift=-0.35cm}, postaction={
        decorate, decoration={markings, 
        mark = at position 0.35 with {\arrow[Aquamarine, rotate=-90]{latex}},
        mark = at position 0.7 with {\arrow[Aquamarine, rotate=-90]{latex}},
                }}] coordinates {(-1, -2) (-1, 9)};
    \addlegendentry{$x_1 \geq -1$}

    % Feasible area
    \filldraw[blue, pattern=north west lines, pattern color=blue] (-1,2+2) -- (-1, 1+2) -- (0, 0+2) -- (1, 1+2) -- (4/3, 2+2) -- cycle;
    \addlegendimage{area legend, pattern=north west lines, pattern color=blue}
    \addlegendentry{Feasible Region}

    % Piecewise linear:
    \addplot[draw=Black, thick, legend image post style={yshift=-0.35cm}] coordinates {(-2, 6) (-1, 3)}; %-3x-2+2
    \addplot[draw=Black, thick] coordinates {(-1, 3) (0, 2)}; %-x+2
    \addplot[draw=Black, thick] coordinates {(0, 2) (1, 3)}; %x+2
    \addplot[draw=Black, thick] coordinates {(1, 3) (2, 6)}; %3x-2+2
    \addlegendentry{$f(x_1) =
        \begin{cases} 
            -3x_1 & \text{if } x_1 \in [-2,-1)\\
            -x_1+2 & \text{if } x_1 \in [-1,0)\\ 
            x_1+2 & \text{if } x_1 \in [0,1)\\ 
            3x_1 & \text{if } x_1 \in [1,2]
        \end{cases} $}

\end{axis}
\end{tikzpicture}

\end{document}

好像:

任何帮助将不胜感激。

答案1

感谢@samcarter_is_at_topanswers.xyz 的建议,我找到了解决方案,方法是[text depth=1cm]添加

\addlegendentry{$f(x_1) =
        \begin{cases} 
            -3x_1 & \text{if } x_1 \in [-2,-1)\\
            -x_1+2 & \text{if } x_1 \in [-1,0)\\ 
            x_1+2 & \text{if } x_1 \in [0,1)\\ 
            3x_1 & \text{if } x_1 \in [1,2]
        \end{cases} $}

所以:

\addlegendentry[text depth=1cm]{$f(x_1) =
        \begin{cases} 
            -3x_1 & \text{if } x_1 \in [-2,-1)\\
            -x_1+2 & \text{if } x_1 \in [-1,0)\\ 
            x_1+2 & \text{if } x_1 \in [0,1)\\ 
            3x_1 & \text{if } x_1 \in [1,2]
        \end{cases} $}

并通过legend image post style={yshift=-0.35cm}删除

\addplot[draw=Black, thick, legend image post style={yshift=-0.35cm}] coordinates {(-2, 6) (-1, 3)};

改编代码:

\documentclass[a4paper,11pt,titlepage]{report}

\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}

\pgfplotsset{compat=1.12}
\usetikzlibrary{patterns, decorations.markings, arrows}

\begin{document}

\begin{tikzpicture}
\begin{axis}
    [
    scale=1.25,
    xmin = -2,  xmax=2,
    ymin = -1,  ymax=8,
    xlabel = $x_1$,
    ylabel = $f(x_1)$,
    grid = both,
    grid style ={line width=.1pt, draw=darkgray!10},
    major grid style ={line width=.2pt, draw=darkgray!50},
    axis line style = {->},
    axis lines = middle,
    minor tick num = 4,
    enlargelimits={abs=0.5},
    axis line style = {latex-latex},
    samples = 100,
    domain = -3:3,
    legend pos = outer north east,
    legend style = {nodes={align=left,anchor=west,scale=0.7}},
    ]
    % Nonlinear function
    \addplot[draw=red, thick] {x^2+2};
    \addlegendentry{$f(x_1) = x^2+2$}

    % f(x_1) <= 7
    \addplot[draw=Aquamarine, thick, opacity=0.7, postaction={
        decorate, decoration={markings, 
        mark = at position 4/24 with {\arrow[Aquamarine, rotate=-90]{latex}},
        mark = at position 5/6 with {\arrow[Aquamarine, rotate=-90]{latex}},
                }}] coordinates{(-3, 4) (3,4)};
    \addlegendentry{$f(x_1) \leq 4$}

    % x_1 >= -1
    \addplot[draw=Aquamarine, thick, opacity=0.7, legend image post style={rotate=90, xshift=-0.3cm, yshift=-0.35cm}, postaction={
        decorate, decoration={markings, 
        mark = at position 0.35 with {\arrow[Aquamarine, rotate=-90]{latex}},
        mark = at position 0.7 with {\arrow[Aquamarine, rotate=-90]{latex}},
                }}] coordinates {(-1, -2) (-1, 9)};
    \addlegendentry{$x_1 \geq -1$}

    % Feasible area
    \filldraw[blue, pattern=north west lines, pattern color=blue] (-1,2+2) -- (-1, 1+2) -- (0, 0+2) -- (1, 1+2) -- (4/3, 2+2) -- cycle;
    \addlegendimage{area legend, pattern=north west lines, pattern color=blue}
    \addlegendentry{Feasible Region}

    % Piecewise linear:
    \addplot[draw=Black, thick] coordinates {(-2, 6) (-1, 3)}; %-3x-2+2
    \addplot[draw=Black, thick] coordinates {(-1, 3) (0, 2)}; %-x+2
    \addplot[draw=Black, thick] coordinates {(0, 2) (1, 3)}; %x+2
    \addplot[draw=Black, thick] coordinates {(1, 3) (2, 6)}; %3x-2+2
    \addlegendentry[text depth=1cm]{$f(x_1) =
        \begin{cases} 
            -3x_1 & \text{if } x_1 \in [-2,-1)\\
            -x_1+2 & \text{if } x_1 \in [-1,0)\\ 
            x_1+2 & \text{if } x_1 \in [0,1)\\ 
            3x_1 & \text{if } x_1 \in [1,2]
        \end{cases} $}

\end{axis}
\end{tikzpicture}

\end{document}

结果:

相关内容