pgf-tikz/pgfplots

Axis-wide `mark` setting causes inconsistency between legend and plot

Opened this issue · 2 comments

When using a cycle list that does not set mark and setting it in the axis option, the marker appears in the legend but not in the plot.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
  \begin{axis} [
        cycle list name=color list,
        mark=o,
      ]
    \addplot+ {x^2};
    \addlegendentry{test}
  \end{axis}
\end{tikzpicture}

\end{document}

image

Setting mark in every axis plot instead works.

I'm not sure what is the intended behavior here (I would have expected the axis-wide setting to work, as the cycle list should not override it, but maybe that's not what's supposed to happen) but the inconsistency with the legend surely isn't it.

Axis-wide mark setting will go into every axis style so I think the expected behavior is that it affects both plot and legend.

mark=<value> stores <value> in \tikz@plot@mark. It seems currently some conditional on \tikz@plot@mark (most likely one or more of several \ifx\tikz@plot@mark\pgfutil@emptys) is wrong. It assumes every axis never contains mark setting.

As a more interesting example, if I add \tikzset{mark=x} to your example, inside axis environment and just before \addplot, both plot and legend use the axis-wide mark=o. That is, if \addplot starts with a non-empty \tikz@plot@mark, the axis-wide mark setting is piked up by both plot and legend, and that non-empty \tikz@plot@mark is totally overwritten.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

% equivalent to \begin{axis}[mark=o]
\pgfplotsset{every axis/.style={mark=o}}

\begin{tikzpicture}
  \begin{axis} [
        cycle list name=color list,
        % mark=o,
      ]
    \tikzset{mark=x}
    \addplot+ {x^2};
    \addlegendentry{test}
  \end{axis}
\end{tikzpicture}

\end{document}

image

Not sure if this is the same issue or just related, let me know if I should open a separate ticket. When plotting with an axis wide only marks, plot and legend agree, but a \ref does not; a line is drawn. Again, using every axis plot instead (i.e. setting only marks at \addplot) works.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
  \begin{axis} [only marks]
    \addplot+ {x^2}; \label{plt:test}
    \addlegendentry{test}
  \end{axis}
\end{tikzpicture}

\ref{plt:test}

\end{document}

image