I would like to record a plot's style to the legend without actually drawing the plot.

I know how to do this when the plot style does not have marks, as shown in the first example below.

However, when the plot has marks, as in the second example, the marks still get drawn even though draw=none.

Is there anyway to suppress the marks, while still ensuring that the style gets propagated to the legend? If not, any suggested workarounds?

MWE:

\documentclass[article]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}  % loads newest improved settings

\begin{document}

\begin{tikzpicture}

\begin{axis}

% Use draw=none so that the plot is not drawn, but style is recorded to
% legend entry
\addplot [red, domain=0:3.5, draw=none] {25*x^0.2};
\addlegendentry{first plot}

% Would like to do the same thing with a mark
% But the mark still gets drawn!
\addplot [blue, mark=*, domain=0:3.5, draw=none] {15 - .5*x};
\addlegendentry{second plot}

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

This produces:

enter image description here

What I want is a blank plot with just the legend.

up vote 4 down vote accepted

You can use addlegendimage (in combination with \addlegendentry) to achieve what you want. Just give the option or key--values respectively to the commands argument as you would do to \addplot. Please note that at least one \addplot command is present so that the legend shows up.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}

        \addlegendimage{no markers,red}
        \addlegendentry{first plot}
        \addlegendimage{blue,mark=*,only marks}
        \addlegendentry{second plot}

        \addplot [draw=none] {x};

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

image showing the result of above code

If you are willing to cheat, you could set xmin=0 and just plot samples at={-1}. I stress that this is a cheat, and of course won't work if you wan to use that empty plot for intersections or fillbetween or the like.

\documentclass[article]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}  % loads newest improved settings

\begin{document}

\begin{tikzpicture}

\begin{axis}[xmin=0,xmax=3.5]

% Use draw=none so that the plot is not drawn, but style is recorded to
% legend entry
\addplot [red, domain=0:3.5, draw=none] {25*x^0.2};
\addlegendentry{first plot}

% Would like to do the same thing with a mark
% But the mark still gets drawn!
%\begin{scope}[opacity=0]
\addplot [blue, mark=*,  draw=none,samples at={-1}] {15 - .5*x};
%\end{scope}
%\tikzset{opacity=1}
\addlegendentry{second plot}

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

enter image description here

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.