How I can customize a legend on pgfplots?
There may be an easier way to do this, but this is certainly the most adaptable. (Don't forget to run it twice.)
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
\begin{axis}[name=boundary]
\addplot {x};\label{pgfplots:c1r1}
\addplot {x+4};\label{pgfplots:c2r1}
\addplot {2*x};\label{pgfplots:c1r2}
\addplot {2*x+4};\label{pgfplots:c2r2}
\addplot {4*x};\label{pgfplots:c1r3}
\addplot {4*x+4};\label{pgfplots:c2r3}
\end{axis}
\node[draw,fill=white,inner sep=0pt,above left=0.5em] at (boundary.south east) {\small
\begin{tabular}{ccl}
$ax$ & $ax+4$ \\
\ref{pgfplots:c1r1} & \ref{pgfplots:c2r1} & $a=1$\\
\ref{pgfplots:c1r2} & \ref{pgfplots:c2r2} & $a=2$\\
\ref{pgfplots:c1r3} & \ref{pgfplots:c2r3} & $a=4$
\end{tabular}};
\end{tikzpicture}
\end{document}
In the solution below I've used the idea given by the author himself in Add custom entry into legend in pgfplot, together with the vertical alignment described in Correctly align vertical text on a baseline in pgfplots which gives:
I have aligned the legend cells to the left and put the legend in the south east, but you can, of course, change that as you see fit :)
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
% https://tex.stackexchange.com/questions/84127/correctly-align-vertical-text-on-a-baseline-in-pgfplots
\def\mystrut{\vphantom{hg}}
% https://tex.stackexchange.com/questions/204395/add-custom-entry-into-legend-in-pgfplot
\pgfplotsset{
legend image with text/.style={
legend image code/.code={%
\node[anchor=center] at (0.3cm,0cm) {#1};
}
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend columns=2,
legend style={
font=\mystrut,
legend cell align=left,
},
legend pos=south east,
]
% column 1 heading
\addlegendimage{legend image with text=$ax$}
\addlegendentry{}
% column 2 heading
\addlegendimage{legend image with text=$ax+4$}
\addlegendentry{}
% the plots of the functions follow
\addplot {x};
\addlegendentry{}
\addplot {x+4};
\addlegendentry{$x+4$}
\addplot {2*x};
\addlegendentry{}
\addplot {2*x+4};
\addlegendentry{$2x+4$}
\addplot {4*x};
\addlegendentry{}
\addplot {4*x+4};
\addlegendentry{$4x+4$}
\end{axis}
\end{tikzpicture}
\end{document}
This solution is very similar to the one given by John Kormylo but uses a \matrix
node from the corresponding library to produce the custom legend.
(This answer is adapted from this duplicate questions answer.)
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% load `matrix' library so we can use the `matrix of nodes' feature
\usetikzlibrary{
matrix,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% just because it looks a bit better than the default
cycle list name=exotic,
]
\addplot {x}; \label{plot:line1}
\addplot {x+4}; \label{plot:line2}
\addplot {2*x}; \label{plot:line3}
\addplot {2*x+4}; \label{plot:line4}
\addplot {4*x}; \label{plot:line5}
\addplot {4*x+4}; \label{plot:line6}
% create a (dummy) coordinate where we want to place the legend
%
% (The matrix cannot be placed inside the `axis' environment
% directly, because then a catcode error is raised.
% I guess that this is caused by the `matrix of nodes' feature)
\coordinate (legend) at (axis description cs:0.97,0.03);
\end{axis}
% create the legend matrix which is placed at the created (dummy) coordinate
% and recall the plot specification using the `\ref' command
%
% adapt the style of that node to your needs
% (e.g. if you like different spacings between the rows or columns
% or a fill color)
\matrix [
draw,
matrix of nodes,
anchor=south east,
] at (legend) {
$ax$ & $ax+4$ & \\
\ref{plot:line1} & \ref{plot:line2} & $a=1$ \\
\ref{plot:line3} & \ref{plot:line4} & $a=2$ \\
\ref{plot:line5} & \ref{plot:line6} & $a=4$ \\
};
\end{tikzpicture}
\end{document}