0

I have an array

devID  = [A, B, C, ..., AP, BP, CP, ...]
device = [A, B, C, ..., A,  B,  C, ...]

which are related to each other.

I want to create another array color which has the same value for those places where device has the same value, i.e.:

color = [ 'r', 'g', 'b', ..., 'r', 'g', 'b', ...]

Since I want to automatize this for plotting different curves associated to devID, which has a length of around 100, I would like the colors to be assigned from, say, a colormap.

So far I've tried different combinations of numpy.unique and numpy.where without any succcess...

acb
  • 177
  • 2
  • 10

3 Answers3

2

As commented, you can use np.unique and index a list of colors with the returned indices.

import numpy as np
import matplotlib.pyplot as plt

device = ['A', 'B', 'C', 'A',  'B',  'C']
u, inv = np.unique(device, return_inverse=True)
colors = plt.cm.viridis(np.linspace(0,1,len(u)))[inv]

print(colors)

prints

[[0.267004 0.004874 0.329415 1.      ]
 [0.127568 0.566949 0.550556 1.      ]
 [0.993248 0.906157 0.143936 1.      ]
 [0.267004 0.004874 0.329415 1.      ]
 [0.127568 0.566949 0.550556 1.      ]
 [0.993248 0.906157 0.143936 1.      ]]
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
1

Create a dictionary for the colors and map them to the device list.

In [25]: device = ['A', 'B', 'C', 'A',  'B',  'C']

In [26]: device
Out[26]: ['A', 'B', 'C', 'A', 'B', 'C']

In [27]: colors = dict(zip(np.unique(device), 'rgb'))

In [28]: colors
Out[28]: {'A': 'r', 'B': 'g', 'C': 'b'}

In [29]: color = np.array([colors[dev] for dev in device])

In [30]: color
Out[30]: array(['r', 'g', 'b', 'r', 'g', 'b'], dtype='<U1')

Replace 'rgb' with iterable of the colors you need. Length must be longer than len(np.unique(device)).

Lists of N colors could be created using

import colorsys
N = 5
HSV_tuples = [(x*1.0/N, 0.5, 0.5) for x in range(N)]
RGB_tuples = list(map(lambda x: colorsys.hsv_to_rgb(*x), HSV_tuples))

(from https://stackoverflow.com/a/876872/10020283)

mcsoini
  • 6,280
  • 2
  • 15
  • 38
  • great, this works. but how can I replace 'rgb' with a colormap (since I need around 100 different colors...) – acb Nov 13 '19 at 14:44
0

An easier way could be using the hue option in the plots of the seaborn package, this option plot elements in different color based on its argument. For example:

import seaborn as sns

devID  = [1, 2, 3, 1, 2, 3]
device = ['A', 'B', 'C', 'A', 'B', 'C']

sns.scatterplot(devID, devID, hue = device)
plt.show()
PabloCarrera
  • 141
  • 4
  • I would love to use pandas in this case, but I'm afraid I can't, or at least I can't think of a way. The plots I'm trying to do are ```a``` vs. ```b```, where both are arrays containing hundreds of values for each ```devID```. I tried to create a pandas dataframe but it was too complicated. But maybe there is a simple way to do that... – acb Nov 13 '19 at 14:50
  • You can just make a pandas dataframe with the arrays, pd.DataFrame({'a' : a, 'b' : b}), but it is not necessary. – PabloCarrera Nov 13 '19 at 15:05
  • But then I would end up with hundreds of dataframes, each one corresponding to one device... how would I manage that? And also, I have to perform some operations on the arrays a and b, like curve fitting and so. Would I be able to do that with dataframes? – acb Nov 14 '19 at 17:30