[Tex/LaTex] Vertical spacing and item separation between entries in pgfplots’ legend

legendpgfplotsspacingtikz-pgf

One could find a lot of questions on how to change the horizontal spacing between legend entries in , but I can't find anything on the vertical spacing between the entries.

For example, when I change the legends font size to something smaller than the axis font size, I'd also like to have a thinner vertical spacing between the entries.

MWE:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
    cells={anchor=west},
    draw=none, fill=none, 
    font=\scriptsize,
    legend pos= north west,
},]

\addplot[blue]{x};
\addplot[red]{2*x};

\legend{$x$,$2x$}

\end{axis}
\end{tikzpicture}
\end{document}

I thought something like

legend style={itemsep = 0.5pt}

could do the trick, but the pgfplots manual remains silent on this point.

enter image description here

Best Answer

You were almost there. Since the pgfplots legend is a matrix, you don't have "items" like with a list, rather rows and columns, so adding the following command does the trick:

legend style={row sep=0.5pt}

Output

figure 1

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}    

\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
    cells={anchor=west},
    draw=none, fill=none, 
    font=\scriptsize,
    legend pos= north west,
    legend style={row sep=0.5pt}
},]

\addplot[blue]{x};
\addplot[red]{2*x};

\legend{$x$,$2x$}

\end{axis}
\end{tikzpicture}
\end{document}