
Ich bin auf ein kleines Problem gestoßen, als ich Diagramme meiner Daten erstellen wollte. Im Grunde arbeite ich an 10 verschiedenen Objekten, die mehrere Eigenschaften haben. Ich habe also eine CSV-Datei mit 10 Zeilen und vielen Spalten, die die Eigenschaften der einzelnen Objekte darstellen. Jede Zeile enthält einige „Leerzeichen“, was bedeutet, dass wir den Eigenschaftswert für dieses Objekt nicht kennen. Dies wird im folgenden MWE durch 4 Objekte A, B, C und D dargestellt, die die Eigenschaften x, y, z haben. Wir kennen x, y und z für A und D, nur x und z für B und x und y für 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}
Dadurch erhalte ich die folgenden Diagramme:
Im linken Diagramm ist die Legende falsch, das braune Quadrat ist tatsächlich das Objekt C und das schwarze Fünfeck das Objekt D (B hat keinen Wert für y). Im rechten Diagramm sollte C nicht erscheinen, da es keinen Wert für die z-Eigenschaft hat.
Ich weiß, dass ich grundsätzlich jede Eigenschaft einzeln betrachten und nur die guten Objekte darstellen könnte, aber ich muss eine Menge verschiedener Eigenschaften darstellen und das würde enorm viel Zeit in Anspruch nehmen.
Würde mich sehr freuen wenn jemand eine Lösung hat :)
Danke
Antwort1
Das Folgende ist eine etwas drastischere Lösung, aber sie funktioniert.
\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}