python-三维数据显示2


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

data = pd.read_csv('C:/Users/zhoubo/Desktop/data/rgbData.csv', header=None)
#print(data)
# get the rgb value
rgbR = data.loc[1:,0:0].values
rgbR = list(map(int, rgbR))

rgbG = data.loc[1:, 1:1].values
rgbG = list(map(int, rgbG))

rgbB = data.loc[1:, 2:2].values
rgbB = list(map(int, rgbB))

indexBg   = rgbR.index(-3)
indexUk   = rgbR.index(-2) + 1
indexFg   = rgbR.index(-1)

# calculate alpha value
a = data.loc[0:0, 3:6].values
a = a.tolist()
a = sum(a,[])
rgbRw = [x * a[0] for x in rgbR]
rgbGw = [x * a[1] for x in rgbG]
rgbBw = [x * a[2] for x in rgbB]

#rgbCal = []
#for i in range(len(rgbRw)):
#    x = -rgbRw[i] - rgbGw[i] - a[3]
#    rgbCal.append(x)

rgbRw = np.array(rgbRw)
rgbGw = np.array(rgbGw)
rgbRw, rgbGw = np.meshgrid(rgbRw, rgbGw)
rgbBcal = -rgbRw - rgbGw - a[3]
#
fig = plt.figure(figsize=(10, 10))
plane = Axes3D(fig)
#plane.plot_surface(rgbRw, rgbGw, rgbBcal)#, 
#
#alpha = np.add(np.array(rgbRw), np.array(rgbGw))
#alpha = np.add(alpha, np.array(rgbBw))
#alpha = alpha.tolist()
#alpha = [x + a[3] for x in alpha]
#alpha = list(map(int, alpha))
##limit the value in range[0,255]
#for x in range(len(alpha)):
#    if alpha[x] < 0:
#        alpha[x] = 0
#    elif alpha[x] > 255:
#        alpha[x] = 255      
#plt.figure(figsize=(10, 10))
#ax = plt.subplot(111, projection='3d')  # 创建一个三维的绘图工程
#  将数据点分成三部分画,在颜色上有区分度
#bg
plane.scatter(rgbR[1:indexBg], rgbG[1:indexBg], rgbB[1:indexBg], 
           c='blue', alpha=0.5, marker='o', label='bg-fg', s =100)  
#unknown
plane.scatter(rgbR[indexBg:indexUk], rgbG[indexBg:indexUk], 
           rgbB[indexBg:indexUk], c='y', alpha=1, marker='^', label='bg-fg') 
#fg
plane.scatter(rgbR[indexUk:indexFg], rgbG[indexUk:indexFg], 
           rgbB[indexUk:indexFg], c='red', alpha=1, marker='+', label='bg-fg', s = 100)

#ax.scatter(rgbR[indexFg + 1], rgbG[indexFg + 1], 
#           rgbB[indexFg + 1], s = 100, c='green', alpha=1, marker='o', label='bg-fg')
 
plane.set_xlabel('r')
plane.set_ylabel('g')
plane.set_zlabel('b')  # 坐标轴

plane.view_init(elev=60, azim=40)
#plt.show()

dataAlphafg = pd.read_csv('C:/Users/zhoubo/Desktop/data/alphaData.csv', header=None)
fgalphaValue = dataAlphafg.loc[0:, 0:0].values 
fgalphaValue = list(map(int, fgalphaValue))

fgDis = dataAlphafg.loc[0:, 1:1].values
fgDis = list(map(int, fgDis)) 

plt.figure(figsize=(10, 10))
alphaDraw = plt.subplot(221) 
x = list(range(len(fgalphaValue)))
alphaDraw.scatter(x, fgalphaValue[0:len(fgalphaValue)],  c='#00ff00', alpha=1, marker='o', label='alpha', s =50)
alphaDraw.set_xlabel('num')
alphaDraw.set_ylabel('alpha')

alphaHist = plt.subplot(222)
num_bins = 256 
alphaHist.hist(fgalphaValue, num_bins, facecolor='red')
alphaHist.set_title('alpha frequency',fontsize=12,color='r')

disHist = plt.subplot(223)
disHist.hist(fgDis, num_bins, facecolor='purple')
disHist.set_title('distance to param plane',fontsize=12,color='r')

vio = plt.subplot(224)
vio.violinplot(fgalphaValue)
vio.set_title('feature distribution')


#index_train = indexUk + (indexFg - indexUk)//2
#trainx = list(zip(rgbR[indexUk:index_train], rgbG[indexUk:index_train], rgbB[indexUk:index_train]))
#testx = list(zip(rgbR[index_train:indexFg], rgbG[index_train:indexFg], rgbB[index_train:indexFg]))
#
#X_scaler = StandardScaler()
#trainx = X_scaler.fit_transform(trainx)
#pca = PCA(n_components=1)
#pca.fit(trainx)
#Z=pca.transform(trainx)
#print(Z)

#figfit = plt.figure()
#planeFit = Axes3D(figfit)

plane.plot_trisurf(rgbR[indexUk:indexFg], rgbG[indexUk:indexFg], rgbB[indexUk:indexFg], alpha = 0.5)

lineFitR =  rgbR[indexBg:indexUk];#indexUk:indexFg
lineFitG =  rgbG[indexBg:indexUk];
lineFitB =  rgbB[indexBg:indexUk];

#lineFitR.insert(0, rgbR[indexBg//2])
#lineFitG.insert(0, rgbG[indexBg//2])
#lineFitB.insert(0, rgbB[indexBg//2])
##indexBg + 1:indexUk -2
##bgSource = list(zip(rgbR[indexBg // 2], rgbG[indexBg // 2], rgbB[indexBg // 2]))
#for  i in range(0, len(lineFitR), 50):
#   linePairR = [lineFitR[0],lineFitR[i]]
#   linePairG = [lineFitG[0],lineFitG[i]]
#   linePairB = [lineFitB[0],lineFitB[i]]
#   plane.plot(linePairR[0:2], linePairG[0:2], linePairB[0:2])
   
#polynomial fitting
 
#lineFitRplane =  rgbR[indexBg:indexUk];#indexUk:indexFg
#lineFitGplane =  rgbG[indexBg:indexUk];
#lineFitBplane =  rgbB[indexBg:indexUk];
#
#plt.figure(figsize=(10, 10))
##points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])
### get x and y vectors
##x = points[:,0]
##y = points[:,1]
#
## calculate polynomial
#def fit_func(x, y, a, b, c, d, e, f):
#    return a*x*x + b*y*y + c*x*y + d*x + e*y + f
#
#curve_fit(fit_func, lineFitRplane, lineFitGplane, lineFitBplane)

#z = np.polyfit(lineFitRplane, lineFitGplane, 2)#lineFitBplane,
#f = np.poly1d(z)

plt.show()

python-三维数据显示2_第1张图片

你可能感兴趣的:(python)