I have code that generates images with filled circles, that can be one of 10 colors using plt.fill
, with the X and Y coordinates of one circle
at a time from circle_list
:
for i in range(0, R.shape[0]):
radius = R[i]
circle = circle_list[i]
color = assigned_colors[i]
plt.fill(circle[:, 0], circle[:, 1], facecolor=color)
assigned_colors
is a list of the same length as R.shape[0]
(the total number of circles to draw) with one of the colors specified in color_list
:
color_list = [(.14,.33,.49), (.08,.41,.27), (.37,.76,.35), (.92,.58,.13),
(.90,.25,.09), (.96,.75,.12), (.74,.09,.15), (.80,.76,.56),
(.56,.70,.72), (.77,.71,.76)]
I then save this image as a numpy ndarray:
plt.savefig('test.png', dpi=350)
arr = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
arr = arr.reshape(fig.canvas.get_width_height()[::-1] + (3,))
np.save('arr.npy', arr)
This all works great, but I would for some analysis I do, I would like to use the unique RGB values specified for filling the circles as unique IDs. However, when I load the array, and look at the unique colors, I observe many more than expected:
circles_npy = np.load('./arr.npy')
len(np.unique(circles_npy.reshape(-1, circles_npy.shape[2]), axis=0))
>>> 2854
If I draw the image, it visually looks like the 10 colors I specified are drawn, but why are the RGB values so different? Is there a way to force matplotlib to only use those exact values, plus a background color? I would have expected this to return 11 (for the 10 possible colors plus the white background. I don't understand what's going on here and how can I use colors as labels in the array? Ideally what I am looking for is a unique value for every color in the image array.