0

I have points in R clusters. I want create a figure, iterate these R clusters and in each iteration i, draw points (scatter) from cluster i with color, that'll be perceptually different from colors of points in other clusters.

In Octave/Matlab, I'd just do

colors = hsv(R);
figure; hold on;
for i = 1:R
    ...
    c = colors(i,:);
    % draw with color c
    ...
end

and each line/set of points would be easily distinguishable from others in the resulting figure. I'm missing this magical hsv(n) function in Matplotlib. I was surprised that I couldn't google it in less than 5 minutes for Matplotlib so hopefully, it'll serve as a reference for other lazy ones.

EDIT:

@ImportanceOfBeingErnest is correct. Also, Matplotlib, unlike Matlab, assigns different color for each plot operation: https://stackoverflow.com/a/16006929/214720

woky
  • 4,686
  • 9
  • 36
  • 44

1 Answers1

2

The hsv colormap in matplotlib is named (oh wonder) hsv. For a reference of all colormaps see Colormap reference.

Note that colormaps in matplotlib range between 0 and 1. Hence, you may need to normalize the input for applying a colormap.

import matplotlib.pyplot as plt
import numpy as np

R = np.linspace(0,1)
color=plt.cm.hsv(R)

or

R = np.linspace(-3,42)
norm= plt.Normalize(-3,42)
color=plt.cm.hsv(norm(R))
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712