# Two columns in legend in pgfplots

I know how to create a legend in a pgfplots plot. See

Legend in tikzpicture

and

But I do not know how I can create a legend with two columns.

-
Have you tried using \multicols environment inside your legend code? –  Thanos Nov 3 '12 at 10:00
No, because I actually don't understand the code of the definition of the legend environment. –  Jana Nov 3 '12 at 10:09
Try to enclose your \addlegendimages in a \multicols environment like \begin{multicols}{2} \addlegendimage \addlegendimage \addlegendimage \end{multicols} –  Thanos Nov 3 '12 at 10:17
I can not get it to compile. –  Jana Nov 3 '12 at 10:28

Simply set the key legend columns=2.

Depending on what you need, you might also be interested in transpose legend and/or reverse legend.

EDIT:

in order to customize the legend's appearance, you can use legend style={<option list>} where <option list> can be any option which applies to a PGF \matrix. All these options can be found in the PGF manual. The one which is necessary here is the column sep key. We have to restrict it to the second column (unless we also want to customize the column separation between images and text).

Here is an example:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend columns=2,
legend style={
% the /tikz/ prefix is necessary here...
% otherwise, it might end-up with /pgfplots/column 2
% which is not what we want. compare pgfmanual.pdf
/tikz/column 2/.style={
column sep=5pt,
},
},
]
\addlegendentry{$x$}
\addlegendentry{$x$+4}
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7160682678222656, "perplexity": 4741.648964355094}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-42/segments/1414119655159.46/warc/CC-MAIN-20141024030055-00147-ip-10-16-133-185.ec2.internal.warc.gz"}