2

I'm fairly new to Python and I'm struggling annotating plots at the minute. I've come from R so I'm used to the ease of being able to annotate scatterplot points with minimum code.

Code:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt 
import matplotlib as mpl
url = ('https://fbref.com/en/share/nXtrf')
df = pd.read_html(url)[0]
df = df[['Unnamed: 1_level_0', 'Unnamed: 2_level_0', 'Play', 'Perf']].copy()
df.columns = df.columns.droplevel()
df = df[['Player','Squad','Min','SoTA','Saves']]
df = df.drop([25])
df['Min'] = pd.to_numeric(df['Min'])
df['SoTA'] = pd.to_numeric(df['SoTA'])
df['Saves'] = pd.to_numeric(df['Saves'])
df['Min'] = df[df['Min'] > 1600]['Min']
df = df.dropna()
df.plot(x = 'Saves', y = 'SoTA', kind = "scatter")

I've tried numerous ways to annotate this plot. I'd like the points to be annotated with corresponding data from 'Player' column.

I've tried using a label_point function that I've found while trying to find a work around buy I keep getting Key Error 0 on most ways I try.

Any assistance would be great. Thanks.

1 Answer 1

1

You could loop through both columns and add a text for each entry. Note that you need to save the ax returned by df.plot(...).

ax = df.plot(x='Saves', y='SoTA', kind="scatter")

for x, y, player in zip(df['Saves'], df['SoTA'], df['Player']):
    ax.text(x, y, f'{player}', ha='left', va='bottom')
xmin, xmax = ax.get_xlim()
ax.set_xlim(xmin, xmax + 0.15 * (xmax - xmin))  # some more margin to fit the texts

example output

An alternative is to use the mplcursors library to show an annotation while hovering (or after a click):

import mplcursors

mplcursors.cursor(hover=True)

mplcursors example

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

2 Comments

thanks for that, in the first example you gave me. That returns the x,y coordinates and pastes them as text. How would I replace the xy coordinates with text from the 'Players' column in the df?
To avoid overlapping texts, you could try Matplotlib overlapping annotations / text. That library can be installed as pip install adjustText. Note that the mplcursor library also facilitates customizations such as showing a third column.

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.