我正在尝试在python中使用matplotlib创建图像的RGB立方体。我有一个python列表,其中所有像素的RGB格式为[(2,152,255),(0,0,0)...]
。我用散点图绘制所有点,但我不知道如何用各自的RGB颜色绘制每个RGB点。
我尝试做类似ax.scatter(paleta[0],paleta[1],paleta[2],c = RGBlist)
的操作,但是该函数期望RGBa值...
我希望这样的somthig:
代码: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')
最佳答案
尝试将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')
提供输出: