I have a 2D embedding and want a scatter plot colored by label, with a legend.
Answer 1: Matplotlib scatter with a legend per class
Plotting each class separately gives you a proper legend with class names:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 6))
for label in np.unique(y):
mask = y == label
ax.scatter(emb[mask, 0], emb[mask, 1], s=5, alpha=0.7,
label=str(label))
ax.legend(markerscale=3)
ax.set_xlabel("Dimension 1")
ax.set_ylabel("Dimension 2")
plt.show()
alpha makes overlapping points visible, and markerscale enlarges the legend markers so they're readable.
