[Tex/LaTex] Vertical alignment of legend entries in pgfplots

pgfplotsvertical alignment

I have multiline legend entries in my plot. The default styles uses centered alignment for both legend image and legend text. How can I change that such that the legend image is top-alignment next to the legend text?

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        legend style={
            /tikz/nodes={text width=25pt,text depth=},
        },
        legend entries={%
            Some long text,Some other long text
        },
        ]
    \addplot {x};
    \addplot {1+x};

    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Best Answer

You can use anchor=north for each node in the legend. Even better appears to be anchor=base which aligns with the base line of the first line. Since the images have their center on the baseline, we need to shift them somewhat:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        legend style={
            /tikz/every odd column/.style={yshift=2pt},
            /tikz/nodes={text width=25pt,text depth=,anchor=base},
        },
        legend entries={%
            Some long text,Some other long text
        },
        ]
    \addplot {x};
    \addplot {1+x};

    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

My idea assumes that the images are in the odd columns only.

Note: for some reason, the /tikz/ prefix is mandatory in this context (although it usually isn't).