how to give scatter plot point different colors based on value code example
Example 1: scatter plot color by value
x=['A','B','B','C','A','B']
y=[15,30,25,18,22,13]
def pltcolor(lst):
cols=[]
for l in lst:
if l=='A':
cols.append('red')
elif l=='B':
cols.append('blue')
else:
cols.append('green')
return cols
cols=pltcolor(x)
plt.scatter(x=x,y=y,s=500,c=cols)
plt.grid(True)
plt.show()
Example 2: scatter plot color by value
from matplotlib import pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [125, 32, 54, 253, 67, 87, 233, 56, 67]
color = [str(item/255.) for item in y]
plt.scatter(x, y, s=500, c=color)
plt.show()