# How can I set the mark color in PGFPLOTS legend?

Updated: I want to set the color of the mark in the legend to a specific value, but don't know how. The reason for that is that I don't want one legendentry per color (that would be six colors in the MWE below), but one legendentry per "green" and one per "blue".

Here's a litte MWE to demonstrate. I have put a non-working mark color=XXX as addlegendentry option to show what I wanted to do.

\documentclass[paper=a4]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{filecontents,tikz,pgfplots}

\pgfplotsset{mystyle/.style={%
scatter,only marks,scatter src=explicit symbolic,draw opacity=0,mark size=3,%
scatter/classes={ %
a={mark=*,fill=Green},%
b={mark=*,fill=ForestGreen},%
c={mark=*,fill=OliveGreen},%
d={mark=*,fill=Cyan},%
e={mark=*,fill=TealBlue},%
f={mark=*,fill=SkyBlue}%
}}}

\begin{filecontents}{timings.txt}
12   13    2     8    0.223    2.23949e+06    a
13   15    4     8    0.483    2.83788e+11    a
6    7    1     3    0.021    64             b
22   29   10    13    0.689    3.21053e+10    b
22   29   10    13    0.693    3.21053e+10    c
11   14    6    15    0.205    1.65888e+06    c
\end{filecontents}

\begin{filecontents}{timings2.txt}
12   13   26   926   10.966    2.94773e+10   d
13   15  459  4034  317.309    1.02249e+16   d
6    7    4     5    0.029    810           e
22   29  716   841   46.686    2.48052e+15   e
22   29  716   841   46.708    2.48052e+15   f
11   14   43    81    1.056    1.24003e+09   f
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{loglogaxis}
\addplot[mystyle] table[x index=5, y index=4, meta index=6] {timings.txt};
\addplot[mystyle] table[x index=5, y index=4, meta index=6] {timings2.txt};
\addlegendentry[mark color=red]{foo}
\addlegendentry[mark color=black]{bar}
\end{loglogaxis}
\end{tikzpicture}

\end{document}

• The optional argument of \addlegendentry is for the text of the legend entry and not for the legend symbol. So to help you it would be nice if you could edit your question and state a more real MWE, i.e. you mentioned using meta index for the plots which you don't use in your MWE. Thanks in advance. – Stefan Pinnow Mar 19 '18 at 15:44
• And of course your MWE doesn't have any marks at all now. Neither in the plots nor in the legend ... – Stefan Pinnow Mar 19 '18 at 15:57
• @StefanPinnow: Thanks for your comment, I have updated the MWE to (almost) reflect the real situation. – cxxl Mar 20 '18 at 10:30
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Stefan Pinnow Mar 24 '18 at 12:46

## 3 Answers

You could use \addlegendimage I suppose.

\documentclass[paper=a4]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{filecontents,pgfplots}

\pgfplotsset{mystyle/.style={%
scatter,only marks,scatter src=explicit symbolic,draw opacity=0,mark size=3,%
scatter/classes={ %
a={mark=*,fill=Green},%
b={mark=*,fill=ForestGreen},%
c={mark=*,fill=OliveGreen},%
d={mark=*,fill=Cyan},%
e={mark=*,fill=TealBlue},%
f={mark=*,fill=SkyBlue}%
}}}

\begin{filecontents}{timings.txt}
12   13    2     8    0.223    2.23949e+06    a
13   15    4     8    0.483    2.83788e+11    a
6    7    1     3    0.021    64             b
22   29   10    13    0.689    3.21053e+10    b
22   29   10    13    0.693    3.21053e+10    c
11   14    6    15    0.205    1.65888e+06    c
\end{filecontents}

\begin{filecontents}{timings2.txt}
12   13   26   926   10.966    2.94773e+10   d
13   15  459  4034  317.309    1.02249e+16   d
6    7    4     5    0.029    810           e
22   29  716   841   46.686    2.48052e+15   e
22   29  716   841   46.708    2.48052e+15   f
11   14   43    81    1.056    1.24003e+09   f
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{loglogaxis}
\addlegendimage{mark=*,red,only marks,mark size=3}
\addlegendimage{mark=*,black,only marks,mark size=3}

\addplot[mystyle] table[x index=5, y index=4, meta index=6] {timings.txt};
\addplot[mystyle] table[x index=5, y index=4, meta index=6] {timings2.txt};
\addlegendentry{foo}
\addlegendentry{bar}
\end{loglogaxis}
\end{tikzpicture}

\end{document}

• Perfect! The thing that makes the difference for me was that one has to put both \addlegendimage settings before the \addplot directives. – cxxl Mar 20 '18 at 14:52
• @cxxl That, or you can add forget plot to the \addplot options. (\addplot[mystyle,forget plot] ..). – Torbjørn T. Mar 20 '18 at 19:00

An alternative of adding \addlegendimage commands you could also state some dummy scatter classes before your real use scatter classes. (Also you can simplify your code a bit.) For details please have a look at the comments in the code.

% used PGFPlots v1.15
\begin{filecontents}{timings.txt}
12   13    2     8    0.223    2.23949e+06    a
13   15    4     8    0.483    2.83788e+11    a
6    7    1     3    0.021    64             b
22   29   10    13    0.689    3.21053e+10    b
22   29   10    13    0.693    3.21053e+10    c
11   14    6    15    0.205    1.65888e+06    c
\end{filecontents}
\begin{filecontents}{timings2.txt}
12   13   26   926   10.966    2.94773e+10   d
13   15  459  4034  317.309    1.02249e+16   d
6    7    4     5    0.029    810           e
22   29  716   841   46.686    2.48052e+15   e
22   29  716   841   46.708    2.48052e+15   f
11   14   43    81    1.056    1.24003e+09   f
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
only marks,
mark size=3,
scatter src=explicit symbolic,
scatter/classes={
% for the legend entries (only)
dummy1={mark=*,green},
dummy2={mark=*,blue},
% the real scatter classes
a={mark=*,Green},
b={mark=*,ForestGreen},
c={mark=*,OliveGreen},
d={mark=*,Cyan},
e={mark=*,TealBlue},
f={mark=*,SkyBlue}
},
table/x index=5,
table/y index=4,
table/meta index=6,
]
\addplot [scatter] table {timings.txt};
\addplot [scatter] table {timings2.txt};
\legend{
foo,
bar,
}
\end{loglogaxis}
\end{tikzpicture}
\end{document}


One way to achieve this is just to overwrite the line drawn by pgfplots. All one has to do is to give the nodes in the legend a name and then to draw the line in whatever color. I made the line a bit thicker, but this is not necessary, one could also use double or whatever.

\documentclass[paper=a4]{article}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{loglogaxis}
\addplot[domain=1:10^39,green] {x^( 0.09761512598518318 ) * 10^( -1.1943157065805068 ) };
\addplot[domain=1:10^39,blue] {x^( 0.26304620518434785 ) * 10^( -2.117101843033601 ) };
\addlegendentry[name=foo]{\scriptsize foo}
\addlegendentry[name=bar]{\scriptsize bar}
\end{loglogaxis}
\draw[red,thick] (foo) -- ++(-0.9cm,0);
\draw[black,thick] (bar) -- ++(-0.9cm,0);
\end{tikzpicture}
\end{document}