[Tex/LaTex] Use single plot marker in manual “group” legend entries

legendpgfplotstikz-pgf

I have an axis which contains several plots. These plots can be divided into two groups, of which each has a common plot marker. The plots are styled using a combined cycle list.

I would then like to create a legend, which shows all entries without their markers and instead include the markers in manually created "group headlines" inside the legend. Hopefully, my MWE can explain, what I mean:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}

        \begin{axis}[
            ymax=2,
            cycle multiindex* list={
                mark=square,mark=o\nextlist     
                color list
                },
            legend style={mark options={no markers}},
            legend image code/.code={
                \draw[domain=0:0.8, mark repeat=12,mark phase=7, mark size=1.5,#1]
                plot   (\x cm,{0.05cm*sin((\x r)*2*pi/0.8)});
                }
            ]

            \addlegendimage{empty legend} % This should produce a single square marker instead          
            \addlegendentry{Headline 1}             
            \addplot table {
                0 0
                1 1
            };          
            \addlegendentry{Data 1 }

            \addlegendimage{empty legend} % This should produce a single circle marker instead
            \addlegendentry{Headline 2}

            \addplot table {
                0 0
                1 0.5
            };
            \addlegendentry{Data 2}

        \end{axis}


    \end{tikzpicture}
\end{document}

Markers added via MS Paint

These markers are added via MS Paint, to illustrate what I am looking for. I have also redefined legend image code (inspired by the data visualization tikz library) and would like to keep this for reasons of consistency with my other plots. I also do not want my cycle list to be affected by the insertion of the headlines.

What would be the most straightforward way to do this? I have tried to hack myself around it by using "invisible" plots (using only markers and mark options={scale=0}) but this also renders the legend entries invisible.
Do I have to define my own legend style to use in this case?

This could probably be solved, if I set the legend style={mark options={no markers}} individually for every plot instead of the axis options. But this seems a little cumbersome to me and I still wouldn't know how to add invisible plots that only show up inside the legend.

Best Answer

When I understood your question right, you where on the right track. Have a look at the comments in the code for more details.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{
        % create a new style for the "curved line legend"
        /pgfplots/curved line legend/.style={
            legend image code/.code={
                % only a line curved line without markers should be drawn
                \draw [domain=0:0.8,no markers,#1]
                    plot (\x cm,{0.05cm*sin((\x r)*2*pi/0.8)});
            },
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            ymax=2,
            cycle multiindex* list={
                mark=square,mark=o\nextlist
                color list
                },
            ]

            % This should produce a single square marker instead
            \addlegendimage{mark=square,only marks}
            \addlegendentry{Headline 1}

            % Apply the style which should be used for the legend entry
            % if it should be different to the default style to the `\addplot' command
            % in this case it is our previously defined style
            \addplot+ [curved line legend] table {
                0 0
                1 1
            };
            \addlegendentry{Data 1}

            % Same as before
            \addlegendimage{mark=o,only marks}
            \addlegendentry{Headline 2}

            \addplot+ [curved line legend] table {
                0 0
                1 0.5
            };
            \addlegendentry{Data 2}

        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code