2013-05-14 36 views
1

我想在python中用matplotlib創建一個圖像的RGB立方體。我有一個格式爲[(2,152,255),(0,0,0)...]的所有像素的RGB列表。我用散射函數繪製所有點,但我不知道如何繪製各RGB點的RGB顏色。RGB的立方體的圖像

我試着做這樣的事情ax.scatter(paleta[0],paleta[1],paleta[2],c = RGBlist),但功能期望的RGBA值...

我希望somthig這樣的:

enter image description here

CODE:

paleta=zip(*RGBlist) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(paleta[0],paleta[1],paleta[2]) ax.grid(False) ax.set_title('grid on') plt.savefig('Images\\RGBcube.png')

回答

1

嘗試縮放你的RGB值範圍爲[0,1]:

ax.scatter(paleta[0],paleta[1],paleta[2], c=[(r[0]/255., r[1]/255., r[2]/255.) for r in RGBlist])

這工作在下面的例子:

import random 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d, Axes3D 

RGBlist = [(random.randint(0,255), random.randint(0,255), random.randint(0,255)) for i in range(100)] 
paleta=zip(*RGBlist) 
fig = plt.figure() 
ax = Axes3D(fig) 
ax.scatter(paleta[0],paleta[1],paleta[2], c=[(r[0]/255., r[1]/255., r[2]/255.) for r in RGBlist]) 
ax.grid(False) 
ax.set_title('grid on') 
plt.savefig('blah.png') 

給輸出:

enter image description here

+0

這就是我的意思!謝謝 ! – user2383047

0

color屬性scatter功能:

顏色 - matplotlib顏色精氨酸或者RGBA元組的序列