Select a color map , for example viridis :
cmap = plt.get_cmap('viridis')
The cmap color palette is a function that can take an array of values ββfrom 0 to 1 and display them in RGBA colors. np.linspace(0, 1, len(names)) creates an array of identically spaced numbers from 0 to 1 of length len(names) . Thus,
colors = cmap(np.linspace(0, 1, len(names)))
selects colors with the same difference with the viridis color map.
Note that this does not use the value of the string; it only uses the ordinal position of the string in the list to select a color. Also note that these are not random colors; this is just an easy way to generate unique colors from an arbitrary list of strings.
So:
import numpy as np import matplotlib.pyplot as plt cmap = plt.get_cmap('viridis') names = ["bob", "joe", "andrew", "pete"] colors = cmap(np.linspace(0, 1, len(names))) print(colors)

A problem with
clr = {names[i]: colors[i] for i in range(len(names))} ax.scatter(x, y, z, c=clr)
the c ax.scatter parameter expects a sequence RGB (A) of the same length as x , or the same color. clr is a dict, not a sequence. So if colors is the same length as x , then you can use
ax.scatter(x, y, z, c=colors)
unutbu
source share