首页 > 解决方案 > 提高 matplotlib 中的颜色对比度

问题描述

我有一个超过 13000 个像素的散点图,这些像素根据标签分为 50 个集群。在这种情况下,标签代表对应于像素的化学实体(见图)。我通过将标签转换为数字 [0,50] 然后将这些数字更改为 rgb 值,根据标签为像素着色。虽然这可行,但相邻标签之间的对比度不够好。例如,在图中,前四个标签都是蓝色的,而后七个标签是棕色的。这无助于我区分标签。有没有办法可以改善相邻标签之间的颜色对比度?

class_colors=np.arange(0,50) # each integer corresponds to a label
class_colors_float=[]
for i in class_colors:
    class_colors_float.append(round(i/len(class_colors),2))
cmap=plt.cm.get_cmap('Paired')  # choosing a color map
rgba=cmap(class_colors_float)   # converting the color map to rgb values


# creating scatter plot
t=ax.scatter(...,cmap=plt.cm.get_cmap('Paired'))

recs = []  # creating rectange patches for the legends
# coloring each rectangle according to the label it corresponds to

for i in range(0,len(class_colors)):
  recs.append(mpatches.Rectangle((0,0),1,1,facecolor=rgba[i]))

在此处输入图像描述 plt.legend(recs,classes,loc='best',bbox_to_anchor=(1,0,1,1),mode='expand')

标签: pythonmatplotlibcolorsscatter-plot

解决方案


推荐阅读