colors = im.getcolors(w * h)

def hexencode(rgb):
    r = rgb[0]
g = rgb[1]
b = rgb[2]
return '#%02x%02x%02x' % (r, g, b)
for idx, c in enumerate(colors):
    plt.bar(idx, c[0], color=hexencode(c[1]))
plt.show()
Plot image color histogram using matplotlib