2

I'm doing a basic scatterplot based off a dataframe from a csv. I'm not allowed to embed photos yet but here's a little idea of what it looks like:

Rank  Team    Avg Annual Pay  Points
1     Man U       6534654       32
2     Man City    5993000       44

Nothing too crazy, just taking the average annual pay of players for the premier league teams with their point totals up to this point. Rank is based off how much the team is spending.

What I'm trying to do is plot this with the dots of the scatterplot being different colors and have the legend have the dot color and team beside it.

However, when I run my code, I only get one item in the legend and I'm fairly confident the color isn't correct either. Here's a link to what it looks like: capture of result

Here's what my code looks like so far:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm

wagedata = pd.read_csv("premierleagewages.csv")

df = pd.DataFrame(wagedata)


x = df['AVG Annual Per Player']
y = df['Points']
n = df['Team']
b = df['Rank']

fig, ax = plt.subplots()
ax.scatter(x, y, c=b,cmap=cm.nipy_spectral)

lgd = plt.legend(n, loc='center left', scatterpoints=1, fontsize=8, bbox_to_anchor=(1, 0.5))
plt.xlabel('AVG Annual Pay')
plt.ylabel('Points through 19 games')

fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox_inches='tight')
print(plt.show())

I've been looking at stackoverflow for about 3 hours now and I haven't found anything specifically on this. I did find one where someone was asking something similar about a bar graph but the solution didn't help.

I'm also thinking that it might be hard for the colors to line up as I'm calling them based on numbers off Rank instead of setting the colors manually for every team?

Any help would be much appreciated! Also, I'm new around here so if I did something wrong or what not, please let me know!

  • @bazingaa that other post got me most of the way but now the points in that the legend is now full of the different colors but now the actual points on the plot aren't lol. Gotta figure that out now! –  Dec 28 '18 at 15:01
  • Actually, I figured it out, was forgetting the [i] beside the x and y values. Thanks! –  Dec 28 '18 at 15:03

0 Answers0