1

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

Daraan
  • 1,797
  • 13
  • 24

2 Answers2

0

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()
Nejc
  • 220
  • 1
  • 8
0

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)
scale = (data-mini)/(maxi-mini)
colors = cm.rainbow(scale)
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).

enter image description here

The list of cmap is here: https://matplotlib.org/stable/tutorials/colors/colormaps.html#sequential

Vincent Bénet
  • 1,212
  • 6
  • 20