1

First Question so apologies if I mess this up!!

I have csv data like this 3 columns with headers and 10 rows:

player pass_att pass_cmp
Dak Prescott 58 42
Tom Brady 50 32
Jalen Hurts 35 27
Matt Ryan 35 21
Ben Roethlisberger 32 18
Josh Allen 51 30
Zach Wilson 37 20
Sam Darnold 35 24
Kirk Cousins 49 36
Joe Burrow 27 20

I am reading in the data and producing a graph with the pass attempts as the x-axis and the pass completions as the y-axis and I am trying to use the 'player' column as lables for where my x and y data points intersect. I have tried using the annotate() function and the text() function but I cannot seem to pass the 'player' column in so it will read the values as text labels.

nfl_df = pd.read_csv('nfl2.csv') 
nfl_df = nfl_df.sort_values(by ='pass_att', ascending = False)

np_arr = nfl_df.values
x_2 = np_arr[:, 2]
y_2 = np_arr[:, 1]
#z_2 = nfl_df.column('player')
fig_4 = plt.figure(figsize = (6,4))
axes_4 = fig_4.add_axes([2,2,3,3])

axes_4.set_xlabel('Pass Att')
axes_4.set_ylabel('Pass Cmp')
axes_4.set_title('Pass Att vs Pass Cmp')
axes_4.plot(x_2,y_2,z_2)
axes_4.plot(x_2,y_2, color='cyan', alpha = .90, lw = 2, ls = '-.', marker = 'o', markersize = 7, markerfacecolor = 'b')

#plt.annotate(z_2,(x_2,y_2))

Image of plot so far

Any help here would be appreciated and thanks for the help

2
  • The correct way to plot and annotate the dataframe: code and plot with pandas 1.3.3, matplotlib 3.4.3 and using pandas.DataFrame.plot. Commented Oct 17, 2021 at 0:59
  • A bar plot might be better though: code and plot Commented Oct 17, 2021 at 1:06

1 Answer 1

1

An example of annotate use with your data:

fig, ax = plt.subplots(figsize=(15, 10))
x = df['pass_att'].values
y = df['pass_cmp'].values
t = df['player'].values
ax.scatter(x, y)
ax.set_xlabel('Pass Att')
ax.set_ylabel('Pass Cmp')
ax.set_title('Pass Att vs Pass Cmp')
for i, txt in enumerate(t):
    ax.annotate(txt, xy=(x[i], y[i]), xytext=(x[i], y[i]+0.5), ha='center')
plt.show()

Annotate

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

2 Comments

Thanks a million - still learning the basicsII appreciate the help
Glad to help! Good continuation :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.