0

I am trying to plot a legend for my ternary scatterplot, where I have colored the points based on if they are categorized as "Iv", "SK" or "St", but for some reason, only the first point "Iv" shows up in the legend. Does anyone have any idea why this is happening?

t = df.loc['591686B' : '591697C2','C']
l = df.loc['591686B' : '591697C2', 'O']
r = df.loc['591686B' : '591697C2','P']

colors = {'Iv':'red', 'SK':'green', 'St':'blue'}
fig = plt.figure(figsize=(20,10))
ax = plt.subplot(projection='ternary')
plot = ax.scatter(t, l, r, s=50, c=df['Locality'].map(colors), alpha=0.8, edgecolors='black', marker="o", linewidths=0.3) 

plt.grid()
ax.legend(('Iv','SK', 'St'))
ax.set_tlabel('C')
ax.set_llabel('O')
ax.set_rlabel('P')
plt.show()
Saffy
  • 5
  • 3
  • See https://matplotlib.org/stable/gallery/lines_bars_and_markers/scatter_with_legend.html – JohanC Feb 22 '21 at 19:07
  • Sorry, I still don't really understand what to do! – Saffy Feb 23 '21 at 00:13
  • Well, as explained in the linked documents, you need to call scatter 3 times, once for each color. `for lbl in ('Iv','SK', 'St'): filter = (df['Locality'] == lbl); ax.scatter(t[filter], l[filter], r[filter], s=50, c=colors[lbl], label=lbl, ... )` – JohanC Feb 23 '21 at 08:40

0 Answers0