%pylab inline from mpl_toolkits.mplot3d import Axes3D figsize(12,8) colours = np.array([[111, 28, 21], [ 27, 17, 20], [ 79, 23, 17], [185, 125, 50], [155, 76, 32], [ 82, 24, 17], [127, 63, 33], [193, 91, 63], [176, 97, 36], [ 79, 15, 19], [176, 140, 47], [203, 65, 46], [174, 139, 87], [138, 44, 34], [144, 91, 36], [ 86, 21, 16], [123, 44, 32], [109, 44, 30], [ 84, 29, 27], [121, 42, 65], [ 70, 15, 11], [107, 24, 17]])/255.0 # I've normalized the colours between 0 and 1 def display_colours(colours): figsize(13,2) N = colours.shape[0] for i,c in enumerate(colours): ax = subplot(1,N,(i+1)%N, axisbg=c) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() display_colours(colours) figsize(12,8) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(colours[:,0], colours[:,1], colours[:,2], c =colours, s=50 ) plt.xlim(0,1) plt.ylim(0,1) plt.show() from sklearn.decomposition import PCA pca = PCA(n_components=1) # I'm setting n_components to 1 as we are projecting down to only 1 dimension one_d_colours = pca.fit_transform(colours)[:,0] print one_d_colours.shape print one_d_colours ix = np.argsort(one_d_colours) #what I want is the sorted order of elements in 1-dimension, and I'll use that in # ordering the colours. display_colours(colours[ix]) N = 60 colours = np.random.uniform([0.15,0.1,0.5], [.9,0.5,0.7], size=(N,3)) #here are the colours, unsorted display_colours(colours) figsize(12,8) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(colours[:,0], colours[:,1], colours[:,2], c =colours, s=50 ) plt.show() one_d_colours = pca.fit_transform(colours)[:,0] ix = np.argsort(one_d_colours) display_colours(colours[ix])