pandas – scatter plot with different color legend for each point

Question:

Starting from the following example:

fig, ax = plt.subplots()

df = pd.DataFrame({'n1':[1,2,1,3], 'n2':[1,3,2,1], 'l':['a','b','c','d']})

for label in df['l']:

    df.plot('n1','n2', kind='scatter', ax=ax, s=50, linewidth=0.1, label=label)

what I obtained is the following scatterplot:

enter image description here

I’m now trying to set a different color for each of the four points. I know that I can loop over a set of, for instance, 4 colors in a list like:

colorlist = ['b','r','c','y']

but since my real dataset comprise at least 20 different points, I was looking for a sort of “color generator” to loop within it.

Asked By: Fabio Lamanna

||

Answers:

How about this,

enter image description here


Here is the source code,

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib import cm

fig, ax = plt.subplots()

df = pd.DataFrame({'n1':[1,2,1,3], 'n2':[1,3,2,1], 'l':['a','b','c','d']})

#colors = ['b','r','c','y']
nrof_labels = len(df['l'])
colors = cm.rainbow(np.linspace(0, 1, nrof_labels))     # create a bunch of colors

for i, r in df.iterrows():
    ax.plot(r['n1'], r['n2'], 'o', markersize=10, color=colors[i], linewidth=0.1, label=r['l'])

ax.set_xlim(0.5, 3.5)
ax.set_ylim(0.5, 3.5)
plt.legend(loc='best')

plt.show()
Answered By: SparkAndShine

The following method will create a list of colors as long as your dataframe, and then plot a point with a label with each color:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors
import numpy as np
import pandas as pd

fig, ax = plt.subplots()

df = pd.DataFrame({'n1':[1,2,1,3], 'n2':[1,3,2,1], 'l':['a','b','c','d']})

colormap = cm.viridis
colorlist = [colors.rgb2hex(colormap(i)) for i in np.linspace(0, 0.9, len(df['l']))]

for i,c in enumerate(colorlist):

    x = df['n1'][i]
    y = df['n2'][i]
    l = df['l'][i]

    ax.scatter(x, y, label=l, s=50, linewidth=0.1, c=c)

ax.legend()

plt.show()

enter image description here

Answered By: tmdavison

IIUC you can do it this way:

import matplotlib.pyplot as plt
from matplotlib import colors
import pandas as pd

colorlist = list(colors.ColorConverter.colors.keys())
fig, ax = plt.subplots()
[df.iloc[[i]].plot.scatter('n1', 'n2', ax=ax, s=50, label=l,
                         color=colorlist[i % len(colorlist)])
 for i,l in enumerate(df.l)]

colorlist:

In [223]: colorlist
Out[223]: ['m', 'b', 'g', 'r', 'k', 'y', 'c', 'w']

enter image description here

PS colorlist[i % len(colorlist)] – should always remain in the list bounds

Additionally, if df[l] has repeated elements and if the colors have to be assigned accordingly:

import matplotlib.cm as cm
import matplotlib.colors as colors
import numpy as np
import pandas as pd

fig, ax = plt.subplots(figsize=(8,8))

df = pd.DataFrame({'n1':[1,2,1,3], 'n2':[1,3,2,1], 'l':['b','b','c','d']})

l_unq = df['l'].unique()
colormap = cm.viridis
colorlist = [colors.rgb2hex(colormap(i)) for i in np.linspace(0, 0.9, len(l_unq))]

for i,c in enumerate(colorlist):

    x = df[df.l==l_unq[i]].n1
    y = df[df.l==l_unq[i]].n2
    l = l_unq[i]
    ax.scatter(x, y, label=l, s=50, linewidth=0.1, c=c)

ax.set_xlabel('n1')
ax.set_ylabel('n2')
ax.legend()

plt.show()

enter image description here

Answered By: Bhargavi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.