2

Let me first admit that this may be a really dumb question.

I am using matlab2tikz to export some figures from matlab. As I need some legend entries to be unrelated to my actual plots, I use nan-valued plots to create a legend without actually showing any plot.

In matlab, it seems like a not so bad idea. However, when these plots are passed to pgfplots, the compilation fails. If no legend is requested, everything works fine and the plots are discarded, but when trying to get a legend either with legend entries={} or \addlegendentry{} it fails.

I totally understand that if a plot contains nan these points are discarded or jumped based on the unbounded coords key, but why should the compilation fail in this case ?

Side note : I know that the \addlegendimage{} would probably be the best approach here (as proposed in https://tex.stackexchange.com/a/54834/141947), but that would not directly work as the tex file is built through matlab2tikz

% arara: pdflatex
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
            width=4cm,
            height=4cm,
            scale only axis,
            xmin=0,
            xmax=6,
            ymin=-1,
            ymax=1,
            axis x line=bottom,
            axis y line=left,
            legend entries={First,Second}
        ]
        % Works like a charm
        %\addplot [color=black, mark=o, mark options={solid, black}]  coordinates {(0,0) (1,0)};
        %\addplot [color=black, mark=+, mark options={solid, black}]  coordinates {(0,0) (1,0)};
        % Totally fails
        \addplot [color=black, mark=o, mark options={solid, black}]  coordinates {(nan,nan) (nan,nan)};
        \addplot [color=black, mark=+, mark options={solid, black}]  coordinates {(nan,nan) (nan,nan)};
    \end{axis}
\end{tikzpicture}%
\end{document}

EDIT

Remark

Testing other solutions showed that using an

\addlegendimage{<\addplot options>}
\addlegenentry{<legend entry>}

pair after each \addplot leads to a correct compilation

10
  • 1
    How about just plotting them outside the plot range such that they get clipped away: \addplot [color=black, mark=o, mark options={solid, black}] coordinates {(\pgfkeysvalueof{/pgfplots/xmin}-1,\pgfkeysvalueof{/pgfplots/ymin}-1) (\pgfkeysvalueof{/pgfplots/xmin}-2,\pgfkeysvalueof{/pgfplots/ymin}-2)}; \addplot [color=black, mark=+, mark options={solid, black}] coordinates {(\pgfkeysvalueof{/pgfplots/xmin}-1,\pgfkeysvalueof{/pgfplots/ymin}-1) (\pgfkeysvalueof{/pgfplots/xmin}-2,\pgfkeysvalueof{/pgfplots/ymin}-2)};? – user194703 Sep 9 '19 at 16:28
  • That's another possibility indeed, but it felt cleaner to use nan... – BambOo Sep 9 '19 at 16:36
  • Well, nan makes the plot jump over the coordinates, so you have empty plots. In a way this is what you want but it also means that in your application there is no plot and thus no legend. – user194703 Sep 9 '19 at 17:14
  • 2
    I totally agree to Schrödinger's cat's first comment. The simplest attempt is to plot outside the visible are. But it it can be simplified a bit because you need only one coordinate and additionally either x or y needs to be outside the visible are. Thus \addplot [...] coordinates {(\pgfkeysvalueof{/pgfplots/xmin}-1,0}; is totally sufficient. – Stefan Pinnow Sep 9 '19 at 17:57
  • 1
    Christian hasn't commented here, so @-pinging him won't work. I think I would also expect that nan-only plots create proper legend entries (matplotlib and Octave are examples of systems where this happens), so a feature request on SourceForge is an option. – Torbjørn T. Sep 9 '19 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.