I have a numpy array size of 20 and I want to give each element a color when plotting point cloud
data = np.array([1,2,3,4,5,6,7,8,9,10,20, 19, 18, 17, 16, 15, 14, 13,12,11])
colors # different colors
colors[data]
I'd like to create colors so that every element of the array represent a color of the unspecified size of an array
CodePudding user response:
You can create a list of colors, where they change depending on their position in the array like this
import matplotlib.cm as cm
colors = cm.rainbow(np.linspace(0, 1, len(data)))
You can plot data using maplotlib:
import matplotlib.pyplot as plt
plt.scatter(range(len(data)), data, c=colors)
plt.show()
CodePudding user response:
In order to create colors based on values (not on index)
import numpy
import matplotlib.pyplot as plt
import matplotlib.cm as cm
data = numpy.array([1,2,3,4,5,6,7,8,9,10,20, 19, 18, 17, 16, 15, 14, 13,12,11])
mini = min(data)
maxi = max(data)
colors = cm.rainbow((data-mini)/(maxi-mini))
plt.scatter(range(len(data)), data, c=colors)
plt.show()
The formulla (data-mini)/(maxi-mini)
will create a 1D array between 0 (for min) and 1 (for max).