# pgfplots: Incorrect legend marks for a line plot with markers on top

According to the pgfplots manual (§4.9.5), every legend image post can be used to draw “a line plot and [have] you plot selected markers on top of it.” In that section, they provide an example for a single plot + markers. However, when I try to extend their example to a figure with two plots + markers, I get the wrong marker type in the legend.

In the following MWE, I expected the legend for the “2nd Parabola” to show a square instead of a circle. How can I get the right marker to show up in the legend?

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend image post style={mark=*}]
\addplot+[only marks,forget plot] coordinates {(0.5,0.75) (1,1) (1.5,0.75)};

\addplot+[only marks,forget plot] coordinates {(0.5,1.75) (1,2) (1.5,1.75)};
\end{axis}
\end{tikzpicture}
\end{document}


• you might have a look here: tex.stackexchange.com/questions/134346/… (might be helpful). Feb 19, 2019 at 13:14
• A solution is add legend image post style={mark=[correct mark]} to every last \addplot+, but it would be nicer if the correct mark is automatically selected. Feb 19, 2019 at 15:04
• May be you could post it as your answer and accept it, if it works ;). Feb 19, 2019 at 16:01
• I will add it as an answer in a day or so if nobody else can find a better solution in which one doesn’t need to (re)specify the marker for each legend. :-) Feb 19, 2019 at 17:28

As OP already stated in the comment below the question one could add legend image post style={mark=<correct mark>} to "every" \addplot command, but would be rather lengthy. To shorten this a bit it would be easier to create a custom style with an argument, which I show in the first/left solution.

Another option would be to add some dummy plots first which have the right style, but when you want to make it work in an almost fully automatic manner, then this requires that you strictly use cycle list members in the given order. This is shown in the lower/right solution.

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

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% create a cycle list to show that this is a general solution
cycle multiindex list={
[3 of]mark list\nextlist
exotic\nextlist
linestyles\nextlist
},
% create a style for the "mark" \addplots
my mark style/.style={
only marks,
forget plot,
},
% create a style for the "line" \addplots
my line style/.style={
mark=none,
legend image post style={
% add a parameter here so this can be used to provide the
% right mark (which is shorter than providing the whole key--value)
mark=#1,
},
},
% give a default value to the style (in case no argument is given)
my line style/.default=o,
% create another style to add the dummy legend entries
execute at begin axis={
% add the number of dummy plots for the legend outside the visible area ...
\foreach \i in {1,...,\LegendEntries} {
}
% ... and shift the cycle list index back to 1
\pgfplotsset{cycle list shift=-\LegendEntries}
},
},
}
\begin{document}
% semi automatic solution where still the right mark has to be provided
\begin{tikzpicture}
\begin{axis}[
% (I moved the common \addplot options here)
smooth,
domain=0:2,
% (the \vphantom is just to make both titles appear on the same baseline)
title={Semi automatic solution\vphantom{y}},
]
% use/apply the above created styles
\addplot+ [my mark style] coordinates {(0.5,0.75) (1,1) (1.5,0.75)};

\addplot+ [my mark style] coordinates {(0.5,1.75) (1,2) (1.5,1.75)};
\end{axis}
\end{tikzpicture}
% Almost fully automatic solution where a number of dummy plots has to be given
% to create the required legend.
% An requirement to make this work is that you strictly use a cycle list!
\begin{tikzpicture}
% set here the number of legend entries you want to show
\pgfmathtruncatemacro{\LegendEntries}{2}
\begin{axis}[
smooth,
domain=0:2,
%
% because we need to add the dummy plots somewhere outside the visible
% area we need to set at least one limit explicitly ...
ymin=0,
% ... and also apply the default enlargement
enlarge y limits=0.1,
title={Almost fully automatic solution},
% the style names says everything already ;)
]

% just add the plots (using the styles)
\addplot+ [my mark style] coordinates {(0.5,0.75) (1,1) (1.5,0.75)};

\addplot+ [my mark style] coordinates {(0.5,1.75) (1,2) (1.5,1.75)};
%  stating them at "every" \addplot command)