I would like to annotote my plot by year inside a scatterplot. In addition I would also like to label (add legend) on a different column in a pandas dataframe, in this case the column: ds ['Label']. I have managed to annotate the scatter with the years but I'm stuck on how to label the data from a different column.
Here is my example code
ds
Label Year factor1 factor2 factor3 factor4
0 A 2013 0.318451 0.038893 -0.145478 0.023298
1 B 2013 0.327400 -0.083985 -0.164712 -0.216095
2 C 2013 0.262333 0.251492 0.095186 -0.062729
3 D 2013 0.035074 -0.044357 -0.464473 -0.096461
4 E 2013 0.214464 -0.131810 0.065335 -0.339014
5 F 2013 -0.456510 0.111790 0.358160 0.327663
6 A 2012 0.345147 -0.010345 -0.139058 -0.033598
7 B 2012 0.318605 -0.096974 -0.168039 0.240126
8 C 2012 0.387761 0.145134 0.025229 -0.009165
9 D 2012 -0.007707 -0.033737 -0.401118 0.147932
10 E 2012 0.204582 -0.112144 0.007970 0.367639
11 F 2012 -0.439852 0.128267 0.355429 -0.375302
ds.columnsx=ds['factor2']
y=ds['factor1']
colors = {'A': 'b','B': 'purple', 'C': 'r','D' : 'grey','E' : 'green', 'F' : 'magenta'}
size= 2 *500
x=df['factor2']
y=df['factor1']
labels=df['Year']
fig=figure(1, figsize=(10,8))
ax1 = fig.add_subplot(111)
ax1.scatter(x, y, s=size, alpha=0.7, label=labels, color=[colors[i] for i in ds['Label']])
for label, x, y in zip(labels, x, y):
plt.annotate(label, xy = (x, y),fontsize = 15)
grid(True)
ax1.spines['bottom'].set_color('orange')
ax1.spines['left'].set_color('green')
ax1.xaxis.label.set_color('orange')
ax1.yaxis.label.set_color('green')
ax1.tick_params(axis='x', colors='k')
plt.title('Something', fontsize = 15)
plt.xlabel('Something')
plt.ylabel('Something')
plt.tight_layout()
plt.show()
