create scatter plot with color python 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
import numpy as np
import matplotlib.pyplot as plt
x = np.random.random(10)
y = np.random.random(10)
plt.scatter(x, y, c=y, s=500)
plt.gray()
plt.show()