在三维散点图中标记异常值上的数据标签

时间:2013-05-07 19:27:33

标签: r python-2.7 numpy matplotlib scatter-plot

我有一个标签分隔的数据集,看起来像这样

Labels  t1  t2  t3
gene1   0.000000E+00    0.000000E+00    1.138501E-01
gene2   0.000000E+00    0.000000E+00    9.550272E-02
gene3   0.000000E+00    1.851936E-02    1.019907E-01
gene4   8.212816E-02    0.000000E+00    6.570984E+00
gene5   1.282434E-01    0.000000E+00    6.240799E+00
gene6   2.918929E-01    8.453281E-01    3.387610E+00
gene7   0.000000E+00    1.923038E-01    0.000000E+00
gene8   1.135057E+00    0.000000E+00    2.491100E+00
gene9   7.935625E-01    1.070320E-01    2.439292E+00
gene10  5.046790E+00    0.000000E+00    2.459273E+00
gene11  3.293614E-01    0.000000E+00    2.380152E+00
gene12  0.000000E+00    0.000000E+00    1.474757E-01
gene13  0.000000E+00    0.000000E+00    1.521591E-01
gene14  0.000000E+00    9.968809E-02    8.387166E-01
gene15  0.000000E+00    1.065761E-01    0.000000E+00

我想要的是:获得带有异常值标签的三维散点图,如下所示:

enter image description here

我做了什么:在R

我实际上是这样单独阅读每一列:

library("scatterplot3d")
temp<-read.table("tempdata.txt", header=T)
scatterplot3d(temp1$t1, temp1$t2, temp1$t3)

我想要的是:异常值的标签应至少显示在前250名中,或者我如何在变量中获得前250个异常值的这些标签以供进一步分析。

有人可以在R中指导我。

也欢迎python中的解决方案。

2 个答案:

答案 0 :(得分:1)

将250个标签绘制到绘图中不是一个好选择,因为它会使绘图无法读取。如果要在绘图中标记异常值,则应远离其他数据点,以便轻松识别它们。但是,您可以将最大的250 zz值及其相应的标签保存在矩阵中以供进一步分析。我会做这样的事情:

# Create some random data
library("scatterplot3d")
temp1 <- as.data.frame(matrix(rnorm(900), ncol=3))
temp1$labels <- c("gen1", "gen2", "gen3")
colnames(temp1) <- c("t1", "t2", "t3", "labels")

# get the outliers
zz.outlier <- sort(temp1$t3, TRUE)[1:5]
ix <- which(temp1$t3 %in% zz.outlier)
outlier.matrix <- temp1[ix, ]

# create the plot and mark the points
sd3 <- scatterplot3d(temp1$t1, temp1$t2, temp1$t3)
sd3$points3d(temp1$t1[ix],temp1$t2[ix],temp1$t2[ix], col="red")
text(sd3$xyz.convert(temp1$t1[ix],temp1$t2[ix],temp1$t2[ix]), 
     labels=temp1$labels[ix])

在这里,我还用红色标记了这些点。这将允许您标记比使用文本标签更大量的异常值,同时仍然可以保持绘图的可访问性。但是,如果附近有多个点,它也会失败。

答案 1 :(得分:1)

这是在matplotlib:

import numpy as np
from matplotlib import pyplot, cm
from mpl_toolkits.mplot3d import Axes3D

data = np.genfromtxt('genes.txt', usecols=range(1,4))
N = len(data)
nout = N/4   # top 25% in magnitude
outliers = np.argsort(np.sqrt(np.sum(data**2, 1)))[-nout:]
outlies = np.zeros(N)
outlies[outliers] = 1   # now an array of 0 or 1, depending on whether an outlier

fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(*data.T, c=cm.jet(outlies)) # color by whether outlies.
pyplot.show()

在这里,红色远离原点,附近有蓝色: genes

相关问题