Search code examples
pythonmatplotlibplotrgbcube

RGB cube of an image


I'm trying to create a RGB cube of a image with matplotlib in python. I have a python list with the RGB of all the pixels in format [(2,152,255),(0,0,0)...]. I plot all the points with scatter function, but i don't know how to plot each RGB point with it's respective RGB colour.

I've tried doing something like this ax.scatter(paleta[0],paleta[1],paleta[2],c = RGBlist), but the function expect a RGBa value...

I expect somthig like this:

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')


Solution

  • Try scaling your RGB values to the range [0,1]:

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

    This works in the following example:

    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')
    

    Giving the output:

    enter image description here