Scatter plot color corresponding to value
In all colormap posts, I didn't find the answer, or maybe I didn't understand it.
I want to make a scatter plot with colors.
I have list B:
[1.29,
1.27,
1.46,
0.91,
0.56,
0.99,
1.00,
0.37,
1.24,
1.23]
I'm just using a silly example if you do:
import matplotlib.pyplot as plt
from matplotlib import cm
from math import sin
x=range(10)
y=[sin(i) for i in x]
colors=np.linspace(0,1,10)
plt.scatter(x,y,c=colors,cmap=cm.jet)
You get glasses with different colors, okay.
BUT! I do not want to receive only beautifully painted glasses! I want the points to be colored according to the "intensity" of the values in B.
Here's my silly attempt:
import matplotlib.pyplot as plt
from matplotlib import cm
from math import sin
x=range(10)
y=[sin(i) for i in x]
#colors=np.linspace(0,1,10)
B=[1.29,1.27,1.46,0.91,0.56,0.99,1.00,0.37,1.24,1.23]
plt.scatter(x,y,c=B,cmap=cm.jet)
You get dots colored according to the intensity of the values in B, very nicely:
BUT!! I would like to change the "scale" of the colors to deep blue at 0 and dark red at 2. In this case, the third point (associated with B[2]=1.46
) should be orange, not deep red! How should I do it?
source to share
You need to set the minimum and maximum color manually using the vmin and vmax arguments. For your case it is,
import matplotlib.pyplot as plt
from matplotlib import cm
from math import sin
x=range(10)
y=[sin(i) for i in x]
#colors=np.linspace(0,1,10)
B=[1.29,1.27,1.46,0.91,0.56,0.99,1.00,0.37,1.24,1.23]
cs = plt.scatter(x,y,c=B,cmap=cm.jet,vmin=0.,vmax=2.)
plt.colorbar(cs)
plt.show()
source to share