If I'm doing a scatter plot with the points coloured based on magnitude, can I specify the color levels like in contour & contourf?
For example I do this:
from random import randrange
import matplotlib.pyplot as plt
x, t, t = [], [], []
for x in range(10):
y.append(randrange(30,45,1))
x.append(randrange(50,65,1))
t.append(randrange(0,20,1))
fig = plt.figure()
ax = fig.add_subplot()
scats = ax.scatter(x,y,s=30,c=t, marker = 'o', cmap = "hsv")
plt.show()
Which produces a plot something like this:
But is there some way to add a 'levels' type argument in the scatter plot? There I could specify my levels as being [0,4,8,12,16,20] or something?
Here is a way to do it, also you have multiple errors in your code which I fixed: You define t
twice but don't define y
at all, you override x
with your loop variable, which you should instead replace with _
which is convention in Python for variables that you will not use.
from random import randrange
import matplotlib.pyplot as plt
import matplotlib as mpl
cmap = plt.cm.hsv
bounds = [0,4,8,12,16,20]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
x, y, t = [], [], []
for _ in range(10):
x.append(randrange(30,45,1))
y.append(randrange(50,65,1))
t.append(randrange(0,20,1))
fig = plt.figure()
ax = fig.add_subplot()
scats = ax.scatter(x,y,s=30,c=t, marker = 'o', cmap=cmap, norm=norm)
fig.colorbar(scats)
plt.show()