[英]Coloring in matplotlib scatter plot does not obey the predefined color sequence of my ListedColormap(cmap)
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.具体来说,我使用散点图的参数
c
作为数据标签,然后cmap=ListedColormap(km_colors)
根据我的颜色列表对它们进行着色。 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.但是,看起来颜色图自己决定如何为标记的数据着色,对于两个类,如果
label=1
那么它的颜色是黑色,这也属于我的颜色列表,如果label=0
然后选择最轻的( ?) 我的颜色列表的颜色。 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
.例如,在下面的代码中,您可以看到即使
km_colors[1]='cyan'
,它也会为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()
pca data]: pca数据]:
When an array is provided as an input to ListedColormap()
, the colors in that list are NOT picked up serially.当提供一个数组作为
ListedColormap()
的输入时,该列表中的颜色不会连续拾取。 While I am not aware of the exact process, it usually spreads it between the colors.虽然我不知道确切的过程,但它通常会在颜色之间传播。 So, if there were 6 colors in the list and...
所以,如果列表中有 6 种颜色并且......
and so on...等等...
To fix the colors to be chosen as per your list, you will need to restrict the km_colors
array to the number of colors required.要根据您的列表修复要选择的颜色,您需要将
km_colors
数组限制为所需的颜色数量。 Below is a sample scatter plot with random data created to show how this can be done.下面是一个示例散点图,其中创建了随机数据以显示如何做到这一点。 Note that I am restricting the colors picked up by the scatter plot using
cmap = ListedColormap(km_colors[0:(i*3+j+1)])
, which provides scatter plot with just the first (i*3 + j)请注意,我使用
cmap = ListedColormap(km_colors[0:(i*3+j+1)])
限制散点图拾取的颜色,它仅提供第一个 (i*3 + j) 的散点图
from matplotlib.colors import ListedColormap
x = np.random.rand(100)
y = np.random.rand(100)
km_colors = ['green', 'cyan', 'brown', 'darkorange', 'purple', 'black']
fig, ax = plt.subplots(2,3, sharex='col',figsize = (10,8))
for i in range(2):
for j in range(3):
clr_col = np.random.randint(i*3+j+1, size=(100))
data_PCA = ax[i,j].scatter(x,y, s=55, c=clr_col,
cmap = ListedColormap(km_colors[0:(i*3+j+1)]),
alpha = 0.5)
print(i*3+j+1)
print(np.unique(clr_col))
print(km_colors[0:(i*3+j+1)])
plt.tight_layout()
plt.show()
Output plot输出图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.