pgfplot 中基于网格的图例和线条样式

pgfplot 中基于网格的图例和线条样式

我有以下产生多条线(多项式)的代码

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=$x$,
        ylabel={$f(x)$},
        xmin=-1,xmax=1,
        ymin=-1,ymax=1,
        xtick={0,1}, xticklabels={0,1},
        ytick={0,1}, yticklabels={0,1},
        cycle list name=linestyles,
        legend style={at={(0.02,0.98)}, anchor=north west, draw=none},
        every axis y label/.style={at={(current axis.west)},xshift=-20pt,rotate=90}
    ]
    % use TeX as calculator:
    \addplot+ [mark=none] {x};
    \addplot+ [mark=none, domain=-1:1] {x^2};
    \addplot+ [mark=none, domain=-1:1] {x^3};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^4};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^5};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^6};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^7};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^8};
    \legend{ $x$ \\ $x^2$ \\ $x^3$ \\ $x^4$ \\ $x^5$ \\ $x^6$ \\ $x^7$ \\ $x^8$ \\}
    \end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

我希望将图例放在网格中,这样我就可以更好地利用空间(并且图例不会与线条重叠)。假设在第一行是x和,然后在第二行是和,依此类推。我还希望线条样式不一样x^2x^3x^4没有必须单独向每个添加图添加线条样式。是否有一个循环列表可以满足我的要求?

答案1

您可以通过以下方式将列引入图例legend columns=2。对于合适的间距,您可以使用以下建议这个答案

/tikz/every even column/.append style={column sep=0.5cm}

因为图例图像和文本在分离方面属于独特的列。

完整 MWE

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=$x$,
        ylabel={$f(x)$},
        xmin=-1,xmax=1,
        ymin=-1,ymax=1,
        xtick={0,1}, xticklabels={0,1},
        ytick={0,1}, yticklabels={0,1},
        cycle list name=linestyles,
        legend style={at={(0.98,0.02)}, anchor=south east, draw=none, 
                       /tikz/every even column/.append style={column sep=0.5cm}},
        legend columns=2,
        every axis y label/.style={at={(current axis.west)},xshift=-20pt,rotate=90}
    ]
    % use TeX as calculator:
    \addplot+ [mark=none] {x};
    \addplot+ [mark=none, domain=-1:1] {x^2};
    \addplot+ [mark=none, domain=-1:1] {x^3};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^4};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^5};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^6};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^7};
    \addplot+ [mark=none, domain=-1:1, samples=50] {x^8};
    \legend{ $x$ \\ $x^2$ \\ $x^3$ \\ $x^4$ \\ $x^5$ \\ $x^6$ \\ $x^7$ \\ $x^8$ \\}
    \end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

通过使用addplot而不是addplot+你可以使用自己的循环列表,这里有一个简单的例子:

\documentclass{article}
\usepackage{pgfplots}

\tikzset{
    basiclinestyle/.style = {line width = 0.4mm, solid, line join=round},
}

\pgfplotsset{
    mylinestyles/.style = {
    every axis plot no 0/.append style = {basiclinestyle, color=black, solid},
    every axis plot no 1/.append style = {basiclinestyle, color=black, dash pattern=on 8pt off 1pt},
    every axis plot no 2/.append style = {basiclinestyle, color=black, dash pattern=on 7pt off 2pt},
    every axis plot no 3/.append style = {basiclinestyle, color=black, dash pattern=on 6pt off 3pt},
    every axis plot no 4/.append style = {basiclinestyle, color=black, dash pattern=on 5pt off 4pt},
    every axis plot no 5/.append style = {basiclinestyle, color=black, dash pattern=on 4pt off 5pt},
    every axis plot no 6/.append style = {basiclinestyle, color=black, dash pattern=on 3pt off 6pt},
    every axis plot no 7/.append style = {basiclinestyle, color=black, dash pattern=on 2pt off 7pt},
    every axis plot no 8/.append style = {basiclinestyle, color=black, dash pattern=on 1pt off 8pt},
    }
}

\begin{document}

\begin{figure}
\begin{tikzpicture}
    \begin{axis}[
        mylinestyles,
        xlabel=$x$,
        ylabel={$f(x)$},
        xmin=-1,xmax=1,
        ymin=-1,ymax=1,
        xtick={0,1}, xticklabels={0,1},
        ytick={0,1}, yticklabels={0,1},
        cycle list name=linestyles,
        legend style={at={(0.98,0.02)}, anchor=south east, draw=none, 
                       /tikz/every even column/.append style={column sep=0.5cm}},
        legend columns=2,
        every axis y label/.style={at={(current axis.west)},xshift=-20pt,rotate=90}
    ]
    % use TeX as calculator:
    \addplot [mark=none] {x};
    \addplot [mark=none, domain=-1:1] {x^2};
    \addplot [mark=none, domain=-1:1] {x^3};
    \addplot [mark=none, domain=-1:1, samples=50] {x^4};
    \addplot [mark=none, domain=-1:1, samples=50] {x^5};
    \addplot [mark=none, domain=-1:1, samples=50] {x^6};
    \addplot [mark=none, domain=-1:1, samples=50] {x^7};
    \addplot [mark=none, domain=-1:1, samples=50] {x^8};
    \legend{ $x$ \\ $x^2$ \\ $x^3$ \\ $x^4$ \\ $x^5$ \\ $x^6$ \\ $x^7$ \\ $x^8$ \\}
    \end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容