TikZ:使用散点图和平行坐标绘制相同的数据

TikZ:使用散点图和平行坐标绘制相同的数据

我是 TikZ 的新手,正在尝试重新创建以下图像(在 R 中创建):( 在此处输入图片描述 翻译:相关性 = +1,相关性 = -1,两个聚类,圆形,正态分布)

每个“列”显示相同的数据,使用散点图平行坐标。图形当然相当简单,因此可以使用基本\draw --\fill circle命令手动构建它们。不过,我正在寻找创建、循环和绘制数据的最佳方法,就像在R 源

(注:图片没有必须看看确切地像上面一样,也就是说点的数量并不重要,簇和正态分布中的点可以有不同的坐标,如果圆圈实际上是圆形的,字体是我的 LaTeX 文档中使用的普通字体,那就太好了)

答案1

您可以使用 PGFPlots 来执行此操作。您可以为表中的不同图生成 x 和 y 分量,然后在普通散点图中使用这些分量,或者使用quiver通常用于绘制矢量场的样式来绘制平行图。

\documentclass[landscape]{article}

\usepackage{pgfplots, pgfplotstable}
\begin{document}


% Create a function for generating inverse normally distributed numbers using the Box–Muller transform
\pgfmathdeclarefunction{invgauss}{2}{%
  \pgfmathparse{sqrt(-2*ln(#1))*cos(deg(2*pi*#2))}%
}


% Create the data columns. \pgfplotstablerow is the row index
\pgfplotstablenew[
    create on use/index/.style={
        create col/expr={\pgfplotstablerow}
    },
    create on use/negcorr/.style={
        create col/expr={24-\pgfplotstablerow}
    },
    create on use/clusterx/.style={ % Offset half the data by 4
        create col/expr={rand+(\pgfplotstablerow<12)*4}
    },
    create on use/clustery/.style={
        create col/expr={rand+(\pgfplotstablerow<12)*4}
    },
    create on use/circlex/.style={
        create col/expr={cos(\pgfplotstablerow*360/24)}
    },
    create on use/circley/.style={
        create col/expr={sin(\pgfplotstablerow*360/24)}
    },
    create on use/normalx/.style={
        create col/expr={invgauss(rnd,rnd)}
    },
    create on use/normaly/.style={
        create col/expr={invgauss(rnd,rnd)}
    },
    columns={index,negcorr,clusterx,clustery,circlex,circley,normalx,normaly}
]{24}\datatable

% Save the datatable to a file so it can be used with the quiver style
\pgfplotstablesave{\datatable}{data.csv}

% Create plot styles for the scatter and parallel plots
\pgfplotsset{
    scatterplot/.style args={#1,#2}{
        width=4cm, height=4cm,
        only marks, mark size=1,
        xtick=\empty, ytick=\empty,
        enlargelimits=false,
        table/x=#1,
        table/y=#2
    },
    parallel/.style args={#1,#2}{
        width=4cm, height=4cm,
        no markers,
        xtick=\empty, ytick=\empty,
        enlargelimits=false,
        table/x expr=24,
        table/y=#1,
        quiver={u=24,v=\thisrow{#2}-y} % The quiver plots use relative coordinates, so we'll have to subtract the y coordinate
    }
}

% Create commands for making the plots easier
\newcommand{\scatterplot}[2]{
    \begin{tikzpicture}[trim axis left, trim axis right]
        \begin{axis}[scatterplot={#1,#2}]
            \addplot [black] table {data.csv};
        \end{axis}
    \end{tikzpicture}
}

\newcommand{\parallelplot}[2]{
    \begin{tikzpicture}[trim axis left, trim axis right]
        \begin{axis}[parallel={#1,#2}]
            \addplot [black] table {data.csv};
        \end{axis}
    \end{tikzpicture}
}



\scatterplot{index}{index}%
\scatterplot{index}{negcorr}%
\scatterplot{clusterx}{clustery}%
\scatterplot{circlex}{circley}%
\scatterplot{normalx}{normaly}%

\parallelplot{index}{index}%
\parallelplot{index}{negcorr}%
\parallelplot{clusterx}{clustery}%
\parallelplot{circlex}{circley}%
\parallelplot{normalx}{normaly}%

\end{document}

答案2

Jake 更进一步,完美地生成了数据表。所以现在这个已经过时了,因为我想举个例子,最后说如果有实际数据会更好但是不要紧 :)

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=5 by 2},view={0}{0},xtick=\empty,ytick=\empty,ztick=\empty,enlargelimits=false]
    \nextgroupplot[view={90}{0}]
        \pgfplotsinvokeforeach{-10,...,10}{
        \addplot3[only marks] coordinates {(0,{#1},{-#1} ) (3,{#1},{-#1} )};
        }
    \nextgroupplot[view={90}{0}]
        \pgfplotsinvokeforeach{-10,...,10}{
        \addplot3[only marks] coordinates {(0,{#1},{-#1} ) (3,{-#1},{#1} )};
        }
    \nextgroupplot[view={90}{0},enlargelimits]
        \pgfplotsinvokeforeach{1,...,15}{
        \addplot3[only marks] coordinates {(0,{3+rand},{3+rand})};
        \addplot3[only marks] coordinates {(0,{-3+rand},{-3+rand}) (3,{-3+rand},{-3+rand})};
        }
    \nextgroupplot[view={90}{0},enlargelimits]
        \pgfplotsinvokeforeach{0,15,...,345}{
        \addplot3[only marks] coordinates {(0,{sin(#1+5)},{cos(#1+5)} ) (3,{sin(#1+95)},{cos(#1+95)} ) };
        }
    \nextgroupplot[view={90}{0}]
        \pgfplotsinvokeforeach{1,...,25}{
        \addplot3[only marks] coordinates {(0,{5*rand},{5*rand} ) (3,{5*rand},{5*rand})};
        }
    \nextgroupplot
        \pgfplotsinvokeforeach{-10,...,10}{
        \addplot3[no marks] coordinates {(0,{#1},{-#1} ) (3,{#1},{-#1} )};
        }
    \nextgroupplot
        \pgfplotsinvokeforeach{-10,...,10}{
        \addplot3[no marks] coordinates {(0,{#1},{-#1} ) (3,{#1},{#1} )};
        }
    \nextgroupplot[enlarge x limits=false]
        \pgfplotsinvokeforeach{1,...,15}{
        \addplot3[no marks] coordinates {(0,{3+rand},{3+rand}) (3,{3+rand},{3+rand})};
        \addplot3[no marks] coordinates {(0,{-3+rand},{-3+rand}) (3,{-3+rand},{-3+rand})};
        }
    \nextgroupplot
        \pgfplotsinvokeforeach{0,15,...,345}{
        \addplot3[no marks] coordinates {(0,{sin(#1+5)},{cos(#1+5)} ) (3,{sin(#1+95)},{cos(#1+95)} ) };
        }
        \nextgroupplot
        \pgfplotsinvokeforeach{1,...,25}{
        \addplot3[no marks] coordinates {(0,{5*rand},{5*rand} ) (3,{5*rand},{5*rand})};
        }
    \end{groupplot}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容