# Bug on legend of scatter plot with pgfplots

I have a problem with the legend in a scatter plot using pgfkeys. The color of the halfsquare sybol in the legend is different from the color defined in the plot, as show in the MWE bellow.

How can I correct this?

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

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
height=6cm,
scatter/classes={%
a={mark=square,draw=blue},%
b={mark=triangle,draw=red},%
c={mark=o,draw=orange},
d={mark=diamond,draw=green},
e={mark=pentagon,draw=magenta},
f={mark=halfsquare*,cyan,draw=cyan}
},
}
\tikzset{every mark/.append style={scale=2},
}
\def\ec{2cm}
% this is the leftmost y axis
\begin{axis}[
xmin=0,xmax=1,%--- CF
xshift=0,%-- CF
width=1.8cm,
hide x axis,
axis y line*=left,
ymin=25, ymax=27.5,
ytick distance=0.5,
title={$V_{max}$ (V)},
legend style={at={(3,0.5)},anchor=west}
]
only marks,
%mark=ball,
scatter,% enable scatter
scatter src=explicit symbolic,% the "color data"
]
table[meta=label] {PlotV.dat};
\legend{a,b,c,d,e,f}
\end{axis}

\end{tikzpicture}

\end{document}


• Welcome! How many users do you think will have PlotV.dat, and how is halfsquare* defined? Please make your example self-contained.
– user194703
Apr 21 '20 at 16:32
• @Schrödinger'scat, while I agree on the principle of your comment, the mark=halfsquare* is defined according to the pgfplots manual (actually part of tikz library plotmarks) Apr 21 '20 at 17:47
• @BambOo Yes, thanks! I found this meanwhile, too, and think that the code for that mark in pgflibraryplotmarks.code.tex may not be perfect.
– user194703
Apr 21 '20 at 17:52

Here is a version that does not have the problem. The cleaner way may be to rewrite the code for that mark in pgflibraryplotmarks.code.tex but at least the following is a workaround. Sadly I had to make up data.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{filecontents*}{PlotV.dat}
x y label
0.1 0.15 a
0.45 0.27 b
0.02 0.17 c
0.06 0.1 d
0.9 0.5 e
0.5 0.3 f
0.85 0.52 a
0.12 0.05 b
0.73 0.45 c
0.53 0.25 d
0.76 0.5 e
0.55 0.32 f
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
height=6cm,
scatter/classes={%
a={mark=square,draw=blue},%
b={mark=triangle,draw=red},%
c={mark=o,draw=orange},%
d={mark=diamond,draw=green},%
e={mark=pentagon,draw=magenta},%
f={mark=halfsquare*,cyan,mark options={cyan,scale=2}}%
},
}
\tikzset{every mark/.append style={scale=2},
}
\def\ec{2cm}
% this is the leftmost y axis
\begin{axis}[
xmin=0,xmax=1,%--- CF
xshift=0,%-- CF
width=1.8cm,
hide x axis,
axis y line*=left,
%ymin=25, ymax=27.5,
ytick distance=0.5,
title={$V_\mathrm{max}$ (V)},
legend style={at={(3,0.5)},anchor=west}
]
only marks,
%mark=ball,
scatter,% enable scatter
scatter src=explicit symbolic,% the "color data"
]
table[meta=label] {PlotV.dat};
\legend{a,b,c,d,e,f}
\end{axis}

\end{tikzpicture}

\end{document}


• Well done ! It's funny that you say sadly I had to make up data, it doesn't seem to require much effort for you to solve tikz questions, so I guess it's a way to force you to work a bit lol Apr 21 '20 at 17:54
• @BambOo The main challenge in this answer was indeed to find some data that can be used here but the answer is far from perfect. To be honest, I do not know what the cleanest way is, but it may be to rewrite parts of pgflibraryplotmarks.code.tex. Some of the things there appear at the first sight redundant.
– user194703
Apr 21 '20 at 17:59
• I guess that coul become an issue on the pgfplots bug tracker Apr 21 '20 at 18:01
• @BambOo Maybe. Or I am missing something obvious. The author of these marks is indicated in the file, so maybe one could contact them. (BTW, did you check out the planets package. Soon you may be able to create your avatar with LaTeX. The cats have already claimed a planet ;-)
– user194703
Apr 21 '20 at 18:04
• Nice idea ! Just updated my avatar ! Apr 21 '20 at 20:20