散点图中点的 Z 顺序

散点图中点的 Z 顺序

我使用 pgfplots 完成了以下散点图:

在此处输入图片描述

每个点的元值代表其“重要性”,因此红点比蓝点更重要。问题是红点和橙点被埋在所有蓝点中,尤其是在 (0,0) 附近。是否可以绘制 z 顺序与其元值成比例的点,即红点和橙点位于蓝点之前?

这是我的代码:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\pgfplotsset{
ytick={0,0.2,...,1},
xtick={0,0.2,...,1},
xmajorgrids=true,
ymajorgrids=true,
major grid style={dashed},
}

\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,colorbar horizontal]
        \addplot[scatter,
                 only marks,
                 mark size=1pt,
                 point meta=explicit,
                 point meta min={0},
                 point meta max={100}] table [col sep=comma, 
                                              x index=0, 
                                              y index=1, 
                                              meta index=2] {csv/scatter.csv};
    \end{axis}
\end{tikzpicture}

\end{document}

scatter.csv 示例:

0.000000,0.000000,53
0.000000,0.000213,1
0.000000,0.007653,1
0.000000,0.017226,1
0.000000,0.030485,1
0.000000,0.032558,1
0.000000,0.034453,1
0.000000,0.039656,1
0.000000,0.050874,1
0.000000,0.061787,1
0.000000,0.065450,1
0.000000,0.151315,1
0.000000,0.184418,1
0.000000,0.201284,1
0.000000,0.217143,1
0.000000,0.250000,4
0.000000,0.255159,1
0.000000,0.257401,1
0.000000,0.268999,1
0.000000,0.331126,1
0.000000,0.333333,3

答案1

正如 Pouya 所建议的,您可以对数据进行排序。他建议,datatool但我将使用包\pgfplotstablesort中的命令。\pgfplotstable

我更改了部分数据,以便更清楚地了解代码的工作原理:

\documentclass{article}
\usepackage{pgfplotstable} 
\usepackage{pgfplots}   

\begin{document}
\pgfplotstableread[col sep=comma]{
X,Y, C
0.000000,0.000000,53
0.000000,0.000213,1
0.000000,0.007653,1
0.000000,0.017226,1
0.000000,0.030485,1
0.000000,0.032558,60
0.000000,0.034453,1
0.000000,0.039656,1
0.000000,0.050874,1
0.000000,0.061787,45
0.000000,0.065450,1
0.000000,0.151315,32
0.000000,0.184418,1
0.000000,0.201284,1
0.000000,0.217143,1
0.000000,0.250000,4
0.000000,0.255159,1
0.000000,0.257401,1
0.000000,0.268999,80
0.000000,0.331126,1
0.000000,0.333333,3
}\data

%get the data and sort by column 'C'
\pgfplotstablesort[sort key={C}]{\sorted}{\data} 

%Comment out next command to type the ordered table
%\pgfplotstabletypeset{\sorted}

\pgfplotsset{
ytick={0,0.2,...,1},
xtick={0,0.2,...,1},
xmajorgrids=true,
ymajorgrids=true,
major grid style={dashed},
}

\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,colorbar horizontal]
        \addplot[scatter,
                 only marks,
                 mark size=1pt,
                 point meta=explicit,
                 point meta min={0},
                 point meta max={100}] table [col sep=comma, 
                                              x index=0, 
                                              y index=1, 
                                              meta index=2] {\sorted};
    \end{axis}
\end{tikzpicture}
\end{document}

最终图形的细节如下

在此处输入图片描述

答案2

您可以(滥用)使用内置方法z buffer=sort:为什么不将颜色数据作为第三维结合使用view={0}{90}(这意味着“从顶部视图”,即从 z = + 无穷大)?

该键z buffer=sort将对键进行排序,使得那些距离查看器“较远”(即z在我们的例子中具有较小值)的键将位于底部,而那些距离查看器“较近”(即z此图中具有较大值)的键将绘制在它们的顶部。

例子:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\pgfplotsset{
ytick={0,0.2,...,1},
xtick={0,0.2,...,1},
xmajorgrids=true,
ymajorgrids=true,
major grid style={dashed},
}

\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,colorbar horizontal,view={0}{90}]
        \addplot3[scatter,
                 only marks,
                 mark size=5pt,
                 z buffer=sort,
                 point meta=z,
                 point meta min={0},
                 point meta max={100}] 
         table[col sep=comma]
    {
0.000000,0.000000,53
0.000000,0.000213,1
0.000000,0.007653,1
0.000000,0.017226,1
0.000000,0.030485,1
0.000000,0.032558,1
0.000000,0.034453,1
0.000000,0.039656,1
0.000000,0.050874,1
0.000000,0.061787,1
0.000000,0.065450,1
0.000000,0.151315,1
0.000000,0.184418,1
0.000000,0.201284,1
0.000000,0.217143,1
0.000000,0.250000,4
0.000000,0.255159,1
0.000000,0.257401,1
0.000000,0.268999,1
0.000000,0.331126,1
0.000000,0.333333,3
0.5,0.5,100
0.51,0.51,0
0.49,0.51,50
    };
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

我在中间添加了三个点,并增加了mark size以验证我是否按正确的顺序排序。关闭z buffer=sort以查看它们是否按出现顺序绘制(即错误)。z 缓冲区隐式地导致正确的排序,并且它的开销最小。

相关内容