I'm having a 2D grid of points where each of the points have a corresponding label, which is in the range [0.0, 5.0]
. Now I want to do the following:
Plot all points in the grid and color them according to their label.
However, I don't want to do this using a scatter plot. I've tried to do it with a contourf and pcolormesh plot:
import matplotlib.pyplot as plt
np.random.seed(1234)
x = np.linspace(-1.0, 1.0, num=5)
xx, yy = np.meshgrid(x, x)
z = np.random.randint(low=0, high=6, size=xx.shape)
levels = np.arange(0, 6)
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 0].contourf(xx, yy, z)
axes[0, 1].contour(xx, yy, z, colors='k')
axes[1, 0].scatter(xx, yy, marker='.', c=z)
axes[1, 1].pcolormesh(xx, yy, z)
plt.show()
How should I specify the levels of the contourf
plot such that I get contour lines separating the labels. (Similar to the pcolormesh plot)
Additionally, how can I fix the color for every label, i.e label 4 should always have color red?
EDIT: This is an example of a contourf
plot which produces too many coloured areas:
Actually, there are only two labels in the grid. However, at the border between the two areas, several additional contour lines are drawn.
For the example above, there should be a single contour line separating the two areas (cyan and blue)
I appreciate any help.