[Tex/LaTex] Combine markers in legend entry

legendpgfplotstikz-pgf

I've searched for this online and on this website but couldn't find anything. I would like to place two markers side by side in a legend entry. MWE:

\documentclass[tikz,border=0pt]{standalone} 
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=$x$,
        ylabel={$y$}
    ]
    \addplot [black,domain=-6:6, samples=100,unbounded coords=jump]{x^2 - x +4};
    \addplot [black,domain=-6:6, samples=100,unbounded coords=jump,forget plot]{x^4 - x +4};
    \addplot [only marks,mark=*]{x^2 - x +4};
    \addplot [only marks,mark=square*]{x^4 - x +4};
    \legend{$Analytical$,$Numerical$,$Numerical$}
    \end{axis}
\end{tikzpicture}
\end{document}

Here is a screenshot with the output. I've overlaid what I am trying to achieve. If this is unclear let me know and I will add clarification.

enter image description here

Best Answer

You can define a new style for a legend entry like combo legend below. That style is used in a \addlegendimage, and forget plot is added to the two existing plots, so they are not included in the legend.

Unrelated note: "Analytical" and "Numerical" are not math expressions, so don't write them in $ ... $. If you want the legend entries in italics, use legend style={font=\itshape}.

output of code

\documentclass[tikz,border=0pt]{standalone} 
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=$x$,
        ylabel={$y$},
        % define plot styles, for convenience
        num1/.style={black,only marks,mark=*},
        num2/.style={black,only marks,mark=square*},
        % make new legend style
        combo legend/.style={
          legend image code/.code={
            \draw [/pgfplots/num1] plot coordinates {(1mm,0cm)};
            \draw plot coordinates {(2.5mm,-3pt)} node {,};
            \draw [/pgfplots/num2] plot coordinates {(4.5mm,0cm)};
          }
        }
    ]
    \addplot [black,domain=-6:6, samples=100,unbounded coords=jump]{x^2 - x +4};
    \addplot [black,domain=-6:6, samples=100,unbounded coords=jump,forget plot]{x^4 - x +4};

    % note forget plot, which means the plot is not included in legend
    \addplot [forget plot, num1]{x^2 - x +4};
    \addplot [forget plot, num2]{x^4 - x +4};

    % adds new "fake plot" that is included in legend
    \addlegendimage{combo legend}

    \legend{Analytical,Numerical}
    \end{axis}
\end{tikzpicture}
\end{document}