# Two legend images for one marker in pgfplots

A similar question has been asked here: Pgfplots: let legend show two linestyles

I have a two figures side by side that are plotted in different colors but share the same legend. I would like the legend to have two identical legend images above and below eachother in the respective colors for each entry, but only display the text next to it once. Jesses answer in the above link partly works, but it sets every legend image to whatever is written in 'legend image code', and does not display the marks on the lines. Currently I have settled on making the legend images grey, but its not ideal. How can the answer by Jesse be altered to duplicate each legend image in black and blue, without changing the style at all?

\documentclass{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[width=0.38\linewidth, height=4cm,xmin=0,xmax=10,ymin=0,ymax=10,
xlabel={x}, ylabel = {y}, title={Black plot}, scale only axis,
legend columns=-1, legend style={at={(0,-0.4)}, /tikz/every even column/.append style={column sep=0.5cm}, legend cell align={left}, anchor=north west}]
\addlegendimage{color=gray, mark=x, densely dotted, mark options={solid}}

\addplot[mark=x, densely dotted, mark options={solid}, samples=10] {3*x}[color=black];
\end{axis}

\begin{axis}[at={(1.south east)},
xshift=2cm, width=0.38\linewidth, height=4cm,xmin=0,xmax=10,ymin=0,ymax=10,
title={Blue plot},
scale only axis,
xlabel={x},
ylabel={y}]
\addplot[color=blue, mark=x, dashed, opacity=0.5, samples=10] {2*x+2};
\addplot[color=blue, mark=x, densely dotted, mark options={solid}, samples=10] {3*x+2};
\end{axis}
\end{tikzpicture}
\caption{Caption}
\end{figure}

\end{document}

• For me providing gray legend entries is perfectly fine, if -- at another place -- somewhere is written what the different colors are good for. – Stefan Pinnow Apr 23 '18 at 17:12
• Did my answer answer your question? If yes, please consider upvoting (by clicking on the arrows next to the score) it and/or marking it as the accepted answer (by clicking on the checkmark ✓). If not, please edit your question again and state what you are missing. – Stefan Pinnow Apr 26 '18 at 19:35
• Apologies, the answer was useful, thank you. – Trawkley Apr 27 '18 at 11:39

Not exactly what you are searching for, but creating the legend in that way allows to use all automated features of PGFPlots.

If you wish you could simply add another column in the \matrix command to state what the colors are good for. (Have a look at the link.)

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% load some library to create and place the legend
\usetikzlibrary{
calc,
matrix,
}
\pgfplotsset{
compat=1.3,
% create a custom cycle list' to avoid unnecessary repetitions
cycle list/.define={my cycle list}{
mark=x,mark options={solid},dashed,opacity=0.5\\
mark=x,mark options={solid},densely dotted\\
mark=x,mark options={solid},solid\\
},
% define a custom axis' style
my axis style/.style={
width=0.38\linewidth,
height=4cm,
xmin=0,
xmax=10,
ymin=0,
ymax=10,
xlabel={$x$},
ylabel={$y$},
scale only axis,
%
samples=10,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% use defined custom axis' style
my axis style,
%
title={Black plot},
% create a cycle list' with the help of the previous common defined
% cycle list'
cycle multiindex* list={
black\nextlist
my cycle list\nextlist
},
% name the plot to later use it to position the legend
name=left plot,
]
\end{axis}

\begin{axis}[
my axis style,
% position the right plot relative to the left plot
at={(left plot.south east)},
anchor=left of south west,
xshift=5mm,
%
title={Blue plot},
cycle multiindex* list={
blue\nextlist
my cycle list\nextlist
},
name=right plot,
]
\end{axis}

% create the legend matrix by recalling the plot specification using the
% \ref' command
%
% (e.g. if you like different spacings between the rows or columns
%  or a fill color)
\matrix [
draw,
matrix of nodes,
anchor=north,
yshift=-2.5mm,
node font=\small,
] at ($(left plot.below south west)!0.5!(right plot.below south east)$) {
An entry          & Another entry     & Legend entry three \\
\ref{plot:left1}  & \ref{plot:left2}  & \ref{plot:left3}   \\
\ref{plot:right1} & \ref{plot:right2} & \ref{plot:right3}  \\
};

\end{tikzpicture}
\end{document}