3

I have the following code that produces several lines (polynomials)

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

enter image description here

I would like to have the legend in a grid so I can better use the space (and so that the legend doesn't overlap the lines). Say x and x^2 in the first line, then x^3 and x^4 in the second line, and so on. And I would also like to have the line styles not to be the same without having to add individually to each add plot the line style. Is there a cycle list that gives me what I want?

aaragon
  • 3,041

1 Answers1

5

You can introduce columns to your legend by the key legend columns=2. For a suitable spacing you can use the suggestion of this answer:

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

as legend images as well as text belong to unique columns in terms of separation.

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

enter image description here

And by using addplot instead of addplot+ you can use your own cycle list, here a simple example:

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

enter image description here