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
\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)};? – Sep 09 '19 at 16:28nan... – BambOo Sep 09 '19 at 16:36nanmakes 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. – Sep 09 '19 at 17:14nanallows to create graphic objects with all their properties, wihout display. Let's put it this way, I actually need these plots, but the only part I'm interested in is their legend. In the answer you proposed, it's about the same thing, there is no point in plotting something that is out of plot range,nanis just the ultimate out-of-plot range ^^ – BambOo Sep 09 '19 at 17:35nans get skipped over. (As for your remark, this is not too surprising IMHO, you could then just leave the empty plots out and it will still work if you add\addlegendimage{...}and\addlegenentry{<legend entry>}.) – Sep 09 '19 at 17:40\addplot [...] coordinates {(\pgfkeysvalueof{/pgfplots/xmin}-1,0};is totally sufficient. – Stefan Pinnow Sep 09 '19 at 17:57pgfplots, but maybe the standard behaviour is different here. Regarding the remark, it is not surprising at all indeed, just a remark. – BambOo Sep 09 '19 at 17:59\addplot(understand by that of which all coordinates have been filtered away) should totally be forgotten without e.g adding aforget plot– BambOo Sep 09 '19 at 18:16matplotliband Octave are examples of systems where this happens), so a feature request on SourceForge is an option. – Torbjørn T. Sep 09 '19 at 21:22pgfplotstracker – BambOo Sep 09 '19 at 22:17