color points by label python code example
Example 1: how to plot labeled data with different colors
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = [4,8,12,16,1,4,9,16]
y = [1,4,9,16,4,8,12,3]
label = [0,1,2,3,0,1,2,3]
colors = ['red','green','blue','purple']
fig = plt.figure(figsize=(8,8))
plt.scatter(x, y, c=label, cmap=matplotlib.colors.ListedColormap(colors))
cb = plt.colorbar()
loc = np.arange(0,max(label),max(label)/float(len(colors)))
cb.set_ticks(loc)
cb.set_ticklabels(colors)
Example 2: how to plot two different class in different colour in python
groups = data.groupby("Category")
for name, group in groups:
plt.plot(group["X Value"], group["Y Value"], marker="o", linestyle="", label=name)
plt.legend()