How to give multiple colors when plotting clusters?

Chris Staff asked 10 months ago

In the article about Mean Shift Clustering, we generate a plot of the clusters like this:

When you have multiple clusters, how can we give these clusters a color more easily, i.e. by generalizing the plot?

1 Answers
Best Answer
Chris Staff answered 10 months ago

In the article, we generated colors for the samples displayed in the plot based on their P values, where P represents the list of predictions generated by the clustering algorithm.

Of course, P can also be Y, the original list with targets corresponding to the samples.

We can easily generalize generating the list with colors to a multi-color scenario by specifying the colors in a dictionary with indices as the keys. In the mapping performed for generating the colors list, we can then simply return the color for the index in the lambda function.

This way, you can add many colors easily without having to write a lot of if/else statements.

# Color definitions
colors = {
  0: '#b40426',
  1: '#3b4cc0',
  2: '#f2da0a',
  # ... and so on
}

# Generate scatter plot for training data
colors = list(map(lambda x: colors[x], labels))

Your Answer

17 + 10 =