I am trying to plot a matplotlib plot with colors according to names in a pandas DataFrame. Whereby in the x, y plot different name points have a different color.
dataframe:
id x y Names
0 MAC004524 29.137983 11.864633 ACORN-M
1 MAC004525 28.14 11.80 ACORN-M
2 MAC004526 24.14 12.80 ACORN-C
....
code:
names = set(df['Names'])
colors = list(cmap(np.linspace(0, 1, len(names))))
df['color']=0
for a, c in zip(names, colors):
mask = df.loc[df['Names'] == a]
df.loc[mask, 'color'] = c
#but get an error here KeyError: "[('i', 'd') ('x',) ('y',) ('A', 'c', 'o', 'r', 'n')\n ('A', 'c', 'o', 'r', 'n', '_', 'g', 'r', 'o', 'u', 'p', 'e', 'd')\n ('c', 'o', 'l', 'o', 'r')] not in index"
then id like to plot
x = df['x']
y = df['y']
c= df['color']
plt.scatter(x, y, c=c, s=1)
required df:
id x y Names color
0 MAC004524 29.137983 11.864633 ACORN-M [0.267004 0.004874 0.329415 1. ]