Reputation: 1873
I have a data set of ~ 1500 row with 6 variables that i process with PCA and display with :
from matplotlib.mlab import PCA
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
import numpy as np
data = np.array(data)
try:
results = PCA(data)
except:
raise
#this will return an array of variance percentages for each component
print results.fracs
#
print results.Wt
#this will return a 2d array of the data projected into PCA space
print results.Y
x = []
y = []
z = []
for item in results.Y:
x.append(item[0])
y.append(item[1])
z.append(item[2])
fig1 = plt.figure() # Make a plotting figure
ax = Axes3D(fig1) # use the plotting figure to create a Axis3D object.
pltData = [x,y,z]
ax.scatter(pltData[0], pltData[1], pltData[2], 'bo') # make a scatter plot of blue dots from the data
# make simple, bare axis lines through space:
xAxisLine = ((min(pltData[0]), max(pltData[0])), (0, 0), (0,0)) # 2 points make the x-axis line at the data extrema along x-axis
ax.plot(xAxisLine[0], xAxisLine[1], xAxisLine[2], 'r') # make a red line for the x-axis.
yAxisLine = ((0, 0), (min(pltData[1]), max(pltData[1])), (0,0)) # 2 points make the y-axis line at the data extrema along y-axis
ax.plot(yAxisLine[0], yAxisLine[1], yAxisLine[2], 'r') # make a red line for the y-axis.
zAxisLine = ((0, 0), (0,0), (min(pltData[2]), max(pltData[2]))) # 2 points make the z-axis line at the data extrema along z-axis
ax.plot(zAxisLine[0], zAxisLine[1], zAxisLine[2], 'r') # make a red line for the z-axis.
# label the axes
ax.set_xlabel("x-axis label")
ax.set_ylabel("y-axis label")
ax.set_zlabel("y-axis label")
ax.set_title("The title of the plot")
plt.show() # show the plot
What i would like now is for example, how to color the points displayed by a color according to another data variable. For example, if i add a variable called color in the range of [ 'blue', 'red', 'green'] to each row of data, could i use it to display color the points ?
Upvotes: 0
Views: 5526
Reputation: 1150
I think the easiest thing to do would be to, as you say have a list of colors colors=[ 'blue', 'red', 'green',...] for every data point and then plot each point separately:
for i,xi in enumerate(x):
ax.scatter(x[i],y[i],z[i],color=colors[i])
Another way would be to have a look at this question. Here you can assign a number to every point that gets represented as a color on a color map. so you can show a gradient of a given parameter.
Edit: To answer your comment, The color can be given as a tuple of 3 values (RGB) between 0 and 1
from matplotlib.colors import Normalize
xnorm=Normalize(x.min(),x.max())
ynorm=Normalize(y.min(),y.max())
colors=[(xnorm(x[i]),ynorm(y[i]),0) for i in range(x.size)]
ax.scatter(x,y,c=colors)
Upvotes: 1