1

I am trying to annotate data points on the plot shown below

enter image description here

I am unable to position the annotation text such that it is on the right side of the data point. Any hints on how this can be done?

group1=df.groupby(['Year','Olympics','Medal','Hometown'])['Event'].count()
gr1_df=group1.reset_index()

bronze_data=gr1_df[gr1_df['Medal']=='Bronze']
gold_data=gr1_df[gr1_df['Medal']=='Gold']
silver_data=gr1_df[gr1_df['Medal']=='Silver']

plt.figure();
plt.style.use('seaborn');
plt.plot(bronze_data['Medal'],bronze_data['Hometown'],'*',label='Bronze Medals',alpha=0.5,markersize=10);
plt.plot(gold_data['Medal'],gold_data['Hometown'], 'o',label='Gold Medals',alpha=0.5,markersize=10);
plt.plot(silver_data['Medal'],silver_data['Hometown'],'^',label='Silver Medals',alpha=0.5,markersize=10);
plt.xlabel('Medal');
plt.ylabel('Hometown');
plt.legend();

count_data=gr1_df.groupby('Hometown')['Medal'].value_counts()
count_data.columns=['Counts']
df2=pd.DataFrame(count_data)
df2.columns=['Counts']
df2.reset_index(inplace=True)

ax=plt.gca()

for index, row in df2.iterrows():
    ax.annotate(str(row['Counts']),xy=(row['Medal'],row['Hometown']),xytext=(row['Medal'],row['Hometown']),xycoords='data')

1 Answer 1

0

You have to play with the xytext and textcoords arguments of the annotate function. Here is an invocation which write the number on the right side of the point.

ax.annotate(str(row['Counts']),xy=(row['Medal'],row['Hometown']), xytext=(5,-3),textcoords='offset points')

Kind

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

1 Comment

@SanjSam Do not hesitate to approve the answer, letting others know that the question is solved. Kind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.