1

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()

example

1 Answer 1

2

Your question is a bit unclear but I think you want a legend that corresponds to the labels in ds['Label']. The way to do that is to call ax1.scatter once for each set of points, as in this question. For instance:

colors = {'A': 'b','B': 'purple', 'C': 'r','D' : 'grey','E' : 'green', 'F' : 'magenta'}
size= 1000

fig1, ax1 = plt.subplots(figsize=(10,8))

for t in ('A','B','C'):
    ax1.scatter(ds[ds['Label']==t]['factor2'], ds[ds['Label']==t]['factor1'], 
                color=colors[t], 
                label=t,
                s=size, 
                alpha=0.7)

for label, x, y in zip(ds['Year'], ds['factor2'], ds['factor1']):
    ax1.annotate(label, xy = (x + 0.008, y - 0.003),fontsize = 15)
ax1.legend(markerscale=0.2)

will give you a labelled legend. Is that what you're looking for?

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, Thank you so much!!!. Also , do you know a way to keep the legend "bubbles" small while increasing the bubble size inside the scatter?
Use the markerscale argument to the ax1.legend call. I've updated the code in my answer to include it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.