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

BambOo
  • 8,801
  • 2
  • 20
  • 47
  • 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)};? –  Sep 09 '19 at 16:28
  • That's another possibility indeed, but it felt cleaner to use nan... – BambOo Sep 09 '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. –  Sep 09 '19 at 17:14
  • @Schrödinger'scat AFAIK, using (even only) nan allows 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, nan is just the ultimate out-of-plot range ^^ – BambOo Sep 09 '19 at 17:35
  • I'm not an expert but this is definitely not how I interpret e.g. the example on p. 122 of the manual. My interpretation was always that nans 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
  • 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 09 '19 at 17:57
  • @Schrödinger'scat, my previous comment was not limited to pgfplots, 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
  • @StefanPinnow, I also agree, this would be the simplest answer, in order to avoid the compilation error. But I still have cannot figure why an empty \addplot (understand by that of which all coordinates have been filtered away) should totally be forgotten without e.g adding a forget plot – BambOo Sep 09 '19 at 18:16
  • 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 09 '19 at 21:22
  • @TorbjørnT. I added a feature request on the pgfplots tracker – BambOo Sep 09 '19 at 22:17

0 Answers0