Home > Mobile >  Coloring in matplotlib scatter plot does not obey the predefined color sequence of my ListedColormap
Coloring in matplotlib scatter plot does not obey the predefined color sequence of my ListedColormap

Time:07-14

I have an issue when I am trying to use predefined color sequence for the labels of my data. In detail, I am using the parameter c of scatter plot for the labels of my data, and then cmap=ListedColormap(km_colors) for coloring them according to my colors list. However, it looks like the colormap decides for itself how to color the labeled data, for two classes that is if label=1 then it's colored black, which also belongs to my list of colors, and if label=0 then chooses the lightest(?) color of my color list. So, it does not obey to the order of the colors I set.

For example, in the code below you can see that even though km_colors[1]='cyan', it chooses the black color for label=1.

Thanks a lot for any help in advance.

km_colors = ['green', 'cyan', 'brown', 'darkorange', 'purple', 'black']
fig, ax = plt.subplots(3,3, sharex='col',figsize = (10,8))

for i in range(len(data_list)):
    for j in range(len(n_Enm_clusters)):
        ### c = [km_colors[int(l)] for k,l in enumerate(km_Enm_labels_list[i][j])]
        data_PCA = ax[j,i].scatter(PCA_bold[i][:,0],
                                  PCA_bold[i][:,1],
                                  c=km_Enm_labels_list[i][j], s=15,
                                  cmap = mcolors.ListedColormap(km_colors), 
                                  alpha = 0.5)

    # produce a legend with the unique colors from the scatter
        if i == len(data_list)-1:
            legend1 = ax[j,i].legend(*data_PCA.legend_elements(),
                                    loc="lower right", title="edge \n classes", prop={'size': 6})
            ax[j,i].add_artist(legend1)

plt.tight_layout()
plt.show()


enter image description here

  • Related