
データのプロットを作成しようとしたときに、小さな問題が発生しました。基本的に、私は 10 個の異なるオブジェクトを扱っており、これらのオブジェクトには複数のプロパティがあります。そのため、各オブジェクトのプロパティを表す 10 行と多数の列を持つ csv ファイルがあります。各行には「空白」があり、このオブジェクトのプロパティ値が不明であることを意味します。これは、次の MWE で、プロパティ x、y、z を持つ 4 つのオブジェクト A、B、C、D によって表されます。A と D の x、y、z はわかっていますが、B の x と z、C の x と y だけがわかっています。
\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}
左のプロットでは凡例が間違っており、茶色の四角形は実際にはオブジェクト C であり、黒い五角形はオブジェクト D です (B には y の値がありません)。右のプロットでは、C は z プロパティの値がないため、表示されません。
したがって、基本的には各プロパティを個別に取得し、適切なオブジェクトのみをプロットできることはわかっていますが、プロットするさまざまなプロパティが多数あり、それには膨大な時間がかかります。
誰かが解決策を持っていたら本当に嬉しいです :)
ありがとう
答え1
以下は少し強引な解決策ですが、効果はあります。
\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}