# Legend with Markers for Mesh Plot

I use a mesh plot with scatter points (2d) where the colors are determined from a third coordinate of the data as described in this question. The legend however should contain the markers of a curve as icon (not that miniature mesh). How can I achieve that?

Edit: the problems seems to be with legend image code in line legend that does not draw correctly with mesh settings. How do I have to modify a custom legend image code to simply display the current marker in a fixed color (say black)?

My attempt so far

\pgfplotsset{
/pgfplots/pplots legend/.style={
legend image code/.code={
\draw[color=black] (0cm,0cm) to (0.6cm,0cm);
\draw[#1,color=black,mark repeat=1,mark phase=1]
plot coordinates {
(0.3cm,0cm)
};
}}}


failes due to a compile error:

Undefined control sequence. [...] \pgfplotsplothandlermesh@numpoints ->\numcoords

-

With the settings above, the legend inherits the current settings of the plot (which is typically desired). In your case, you need to override the plot handler explicitly by providing sharp plot (or maybe only marks?) to the legend's option list:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\thispagestyle{empty}
\pgfplotsset{
/pgfplots/pplots legend/.style={
legend image code/.code={
\draw[color=black] (0cm,0cm) to (0.6cm,0cm);
\draw[sharp plot,mark=*,color=black,mark repeat=1,mark phase=1]
plot coordinates {
(0.3cm,0cm)
};
}}}

\begin{tikzpicture}
\begin{axis}[legend entries=$x^2y^2$]
\end{axis}
\end{tikzpicture}
\end{document}


Are you sure that you want the \draw[color=black] ... to ; statement in your legend image code? You can probably replace it by

/pgfplots/pplots legend/.style={%
/pgfplots/legend image code/.code={%
\draw[mark repeat=2,mark phase=2,black,sharp plot]
plot coordinates {
(0cm,0cm)
(0.3cm,0cm)
(0.6cm,0cm)%
};%
}%
},


(which is the default line legend, by the way)

-
Exactly what I was looking for, thank you! –  dcn Nov 3 '11 at 20:20