Hi I am trying to create a scatterplot where each X,Y variable combination is of a particular category, so within the scatterplot I would like to have each category with a different color.
I was able to achieve that as per the code below. However the colorbar that I see on the plot would make more sense if it had the category name on it rather than a numerical value.
Any pointers would be greatly appreciated.
I know seaborn could probably make it easier but I am specifically looking for a matplotlib based solution.
import numpy
import pandas
import matplotlib.pyplot as plt
numpy.random.seed(0)
N = 50
_categories= ['A', 'B', 'C', 'D']
df = pandas.DataFrame({
'VarX': numpy.random.uniform(low=130, high=200, size=N),
'VarY': numpy.random.uniform(low=30, high=100, size=N),
'Category': numpy.random.choice(_categories, size=N)
})
colorMap = {}
k = 0
for i in _categories:
colorMap[_categories[k]] = k
k+=1
plt.figure(figsize=(15,5))
plt.scatter(df.VarX, df.VarY, c= df.Category.map(colorMap), cmap='viridis')
plt.colorbar()
plt.show()
This code produces