Problemas de leyenda cuando faltan coordenadas

Problemas de leyenda cuando faltan coordenadas

Encontré un pequeño problema cuando quería hacer gráficos de mis datos. Básicamente, estoy trabajando en 10 objetos diferentes, que tienen varias propiedades. Entonces tengo un archivo csv, con 10 filas y muchas columnas que representan cada propiedad del objeto. Cada fila tiene algunos "espacios en blanco", lo que significa que no conocemos el valor de la propiedad de este objeto. Esto está representado en el siguiente MWE por 4 objetos A,B,C y D que tienen propiedades x,y,z. Conocemos x, y y z para A y D, solo x y z para B y x e y para C.

\begin{filecontents*}{data.csv}
Label,x,y,z
A,1,5,9
B,4,,7
C,6,5,
D,7,3,4
\end{filecontents*}

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false} 
\usepgfplotslibrary{groupplots}
\begin{document}
\pgfplotsset{
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}
 \tikzset{every mark/.append style={scale=3}}
\pgfplotscreateplotcyclelist{mycolorlist}{%
mark=10-pointed star,only marks,point meta=explicit symbolic,blue,every mark/.append style={fill=blue!80!black}\\%
mark=diamond*,only marks,point meta=explicit symbolic,red,every mark/.append style={fill=red!80!black}\\%
mark=square*,only marks,point meta=explicit symbolic,brown!60!black,every mark/.append style={fill=brown!80!black}\\%
mark=pentagon*,only marks,point meta=explicit symbolic,black,every mark/.append style={solid,fill=black}\\%
}
\hspace*{-4cm}
\begin{tikzpicture}
\newcommand{\xx}{x};
\newcommand{\yy}{y};
\begin{groupplot}[group style={group size= 2 by 1,group name=myplot,horizontal sep=2.5cm,vertical sep = 3.5cm},cycle list name=mycolorlist,legend style={at={(1.,0.5)},anchor=west}]
    \nextgroupplot[ylabel = {y},xlabel={x}]
\addplot+[discard if not={Label}{A},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{A}
\addplot+[discard if not={Label}{B},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{B}
\addplot+[ discard if not={Label}{C},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{C}
\addplot+[ discard if not={Label}{D},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{D}
\nextgroupplot[ylabel = {z},xlabel={x}]
\renewcommand{\yy}{z}
\addplot+[discard if not={Label}{A},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{A}
\addplot+[discard if not={Label}{B},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{B}
\addplot+[ discard if not={Label}{C},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{C}
\addplot+[ discard if not={Label}{D},unbounded coords=discard]table[meta=Label,y=\yy,x=\xx,col sep=comma] {data.csv};\addlegendentry{D}
\end{groupplot}
\end{tikzpicture}
\end{document}

Esto me da los siguientes gráficos: ingrese la descripción de la imagen aquí

En el gráfico de la izquierda, la leyenda es falsa, el cuadrado marrón es en realidad el objeto C y el pentágono negro el objeto D (B no tiene valor para y). En el gráfico de la derecha, C no debería aparecer porque no tiene ningún valor para la propiedad z.

Básicamente, sé que podría tomar cada propiedad individualmente y trazar solo los objetos buenos, pero tengo muchas propiedades diferentes para trazar y me llevaría una gran cantidad de tiempo hacerlo.

Estaría muy feliz si alguien tuviera una solución :)

Gracias

Respuesta1

La siguiente es una solución bastante dura, pero funciona.

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false} 
\usepgfplotslibrary{groupplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableset{col sep=comma,string type}
\pgfplotstableread{
Label,x,y,z
A,1,5,9
B,4,,7
C,6,5,
D,7,3,4
}\Table
%\pgfplotstabletypeset[columns/Label/.style={string type}]{\Table}% for debugging
\pgfplotsset{
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}
 \tikzset{every mark/.append style={scale=3}}
\pgfplotscreateplotcyclelist{mycolorlist}{%
mark=10-pointed star,only marks,point meta=explicit symbolic,blue,every mark/.append style={fill=blue!80!black}\\%
mark=diamond*,only marks,point meta=explicit symbolic,red,every mark/.append style={fill=red!80!black}\\%
mark=square*,only marks,point meta=explicit symbolic,brown!60!black,every mark/.append style={fill=brown!80!black}\\%
mark=pentagon*,only marks,point meta=explicit symbolic,black,every mark/.append style={solid,fill=black}\\%
}
\hspace*{-4cm}
\begin{tikzpicture}
\newcommand{\xx}{x};
\newcommand{\yy}{y};
%\pgfplotstablegetrowsof{\Table}
%\let\rows=\pgfplotsretval
\begin{groupplot}[group style={group size= 2 by 1,group name=myplot,horizontal sep=2.5cm,vertical sep = 3.5cm},cycle list name=mycolorlist,legend style={at={(1.,0.5)},anchor=west}]
    \nextgroupplot[ylabel = {y},xlabel={x}]
    \pgfplotsinvokeforeach{0,1,2,3}{
      \pgfplotstablegetelem{#1}{Label}\of{\Table}
      \let\mark=\pgfplotsretval
      \pgfplotstablegetelem{#1}{\xx}\of{\Table}
      \let\x=\pgfplotsretval
      \pgfplotstablegetelem{#1}{\yy}\of{\Table}
      \let\y=\pgfplotsretval
      \ifx\empty\x\else
        \ifx\empty\y\else
          \addplot+[only marks] coordinates {(\x,\y)};
          \addlegendentryexpanded{\mark}
     \fi\fi}
\nextgroupplot[ylabel = {z},xlabel={x}]
\renewcommand{\yy}{z}
\pgfplotsinvokeforeach{0,1,2,3}{
      \pgfplotstablegetelem{#1}{Label}\of{\Table}
      \let\mark=\pgfplotsretval
      \pgfplotstablegetelem{#1}{\xx}\of{\Table}
      \let\x=\pgfplotsretval
      \pgfplotstablegetelem{#1}{\yy}\of{\Table}
      \let\y=\pgfplotsretval
      \ifx\empty\x\else
        \ifx\empty\y\else
          \addplot+[only marks] coordinates {(\x,\y)};
          \addlegendentryexpanded{\mark}
     \fi\fi}
\end{groupplot}
\end{tikzpicture}
\end{document}

manifestación

información relacionada