博主在做PCA的时候,经常遇到2D图无法区分开各个条件的样本,而3D却能直观地感受到样本之间的距离与差异。使用软件是EPD中的IDLE,输入文件为3维的PCAscore结果,参考之前的PCA脚本即可,3D脚本如下:
#!python
import sys, re
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
fh = open("all_scores_18", 'r')
x = []
y = []
z = []
i = 0
for line in fh:
info = line.rstrip().split()
if i == 0:
x.append(info[0])
y.append(info[1])
z.append(info[2])
else:
x.append(float(info[1]))
y.append(float(info[2]))
z.append(float(info[3]))
i+=1
c = ['r' for i in x[1:]] # red 红色
c[2] = c[3] = c[4] = 'g' # green 绿色
c[5] = c[6] = 'b' # blue 蓝色
c[7] = c[8] = 'm' # magenta 洋红
c[9] = c[10] = 'k' # key 黑色
c[11] = c[12] = 'c' # cyan 青色
c[13] = c[14] = c[15] = 'w' # white 白色
c[16] = c[17] = 'y' # yellow 黄色
print c
fig = plt.figure()
ax4 = fig.add_subplot(111, projection='3d')
ax4.scatter(x[1:],y[1:],z[1:], c=c)
ax4.set_xlabel(x[0])
ax4.set_ylabel(y[0])
ax4.set_zlabel(z[0])
plt.show()
图博主就不展示了,有意向的朋友可以试试~