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?


\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, dashed, opacity=0.5}
\addlegendimage{color=gray, mark=x, densely dotted, mark options={solid}}
\addlegendimage{color=gray, mark=x}
\addlegendentry{An entry}
\addlegendentry{Another entry}
\addlegendentry{Legend entry three}

\addplot[mark=x, dashed, opacity=0.5, samples=10] {2*x}[color=black];
\addplot[mark=x, densely dotted, mark options={solid}, samples=10] {3*x}[color=black];
\addplot[mark=x, samples=10] {4*x}[color=black];

\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,
\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};
\addplot[color=blue, mark=x, samples=10] {4*x+2};


enter image description here

  • For me providing gray legend entries is perfectly fine, if -- at another place -- somewhere is written what the different colors are good for. Apr 23, 2018 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. Apr 26, 2018 at 19:35
  • Apologies, the answer was useful, thank you.
    – Modulus
    Apr 27, 2018 at 11:39

1 Answer 1


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
    % load some library to create and place the legend
        % 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={
            scale only 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={
            my cycle list\nextlist
        % name the plot to later use it to position the legend
        name=left plot,
        \addplot {2*x};     \label{plot:left1}
        \addplot {3*x};     \label{plot:left2}
        \addplot {4*x};     \label{plot:left3}

        my axis style,
        % position the right plot relative to the left plot
        at={(left plot.south east)},
        anchor=left of south west,
        title={Blue plot},
        cycle multiindex* list={
            my cycle list\nextlist
        name=right plot,
        \addplot {2*x+2};       \label{plot:right1}
        \addplot {3*x+2};       \label{plot:right2}
        \addplot {4*x+2};       \label{plot:right3}

    % create the legend matrix by recalling 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)
    % (adapted from <https://tex.stackexchange.com/a/348470/95441>)
    \matrix [
        matrix of nodes,
        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}  \\


image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .