I receive the error No handles with labels found to put in legend. when running the code below. How can I add a legend to this scatter plot that shows the color definitions (a red dot for A, blue dot for B, green dot for C)?
### Dummy Dataset
x = [0,1,-1,4,0,2,2,4,2]
y = [1,5,9,2,4,2,5,6,1]
cat = ['A','B','B','B','A','C','A','B','B']
df = pd.DataFrame(list(zip(x,y,cat)), columns =['x', 'y', 'cat'])
### Build color definitions
df.loc[:, 'color'] = df.cat
df.color.replace(['A', 'B', 'C'], ['red', 'blue', 'green'], inplace=True)
display(df)
### Plotting
fig = plt.figure(figsize=(5,5), constrained_layout=True)
gs = fig.add_gridspec(2, 1)
ax1 = fig.add_subplot(gs[0, 0])
ax1.scatter(df.x, df.y, edgecolors = 'none', c = df.color)
ax1.legend(loc='upper left', facecolor='white', frameon=1,
framealpha=1, labelspacing=0.2, borderpad=0.25)


labelkwarg to your call toscatter. If not all, nearly all matplotlib plotting functions takelabelas a parameter according to their docstrings, which you can see in jupyter by executing e.g.,ax1.scatter?. For more advanced legends, the second scatter example in the matplotlib documentation is pretty exhaustive matplotlib.org/gallery/lines_bars_and_markers/… - Paul Hmyplot = ax1.scatter(df...andax1.legend(*myplot.legend_elements(), loc='upper...), which returned the errorUserWarning: Collection without array used. Make sure to specify the values to be colormapped via thec` argument.` - a11