1

I am trying to text the name of each player in the graph. I have created the function below to print a pointplot.

def eficiencia_chart(datos):
    graph=sns.scatterplot('Eficiencia Defensiva','Eficiencia Ofensiva', data=datos, hue='Position')
    plt.xlim(roundup(max(datos['Eficiencia Defensiva'])),rounddown(min(datos['Eficiencia Defensiva'])))
    plt.ylim(rounddown(min(datos['Eficiencia Ofensiva'])),roundup(max(datos['Eficiencia Ofensiva'])))
    for line in range(0,datos.shape[0]):
        graph.text(datos['Eficiencia Defensiva'][line]+0.2, datos['Eficiencia Ofensiva'][line], datos['Nombre'][line], size='medium')
        print(line)
    return graph

But when I execute it, only one name is printing and returns the message: KeyError: 1

enter image description here

By the way I would like to show a pic with the face of the player if possible instead of points. May somebody shed light on it?

William Miller
  • 9,839
  • 3
  • 25
  • 46

1 Answers1

1

You need to do

graph.text(datos['Eficiencia Defensiva'].values[line]+0.2, datos['Eficiencia Ofensiva'].values[line], datos['Nombre'].values[line], size='medium')

instead of

graph.text(datos['Eficiencia Defensiva'][line]+0.2, datos['Eficiencia Ofensiva'][line], datos['Nombre'][line], size='medium')

Because df['key'] gives you a series, which can’t necessarily be accessed by integer indices, but df['key'].values on the other hand gives you a numpy array of the values which of course can be accessed with integer indices.

If that doesn’t work it likely means the shape of datos is not what you expect it to be and you should verify that datos['Eficiencia Ofensiva'].values.shape[0] ==datos.shape[0]`.

William Miller
  • 9,839
  • 3
  • 25
  • 46
  • Hi William, Thank you very much for your quick response. Do you know how to print images for each point instead of points? – Miguel Santos Jan 24 '20 at 11:19
  • 2
    Are you asking how to do something like [this](https://stackoverflow.com/a/53851017/10659910)? – William Miller Jan 24 '20 at 11:21
  • Not exactly. I would like to represent each point with a face (because at the end are basketball players). In these case, all cases are represented with the same picture. – Miguel Santos Jan 24 '20 at 11:24
  • 2
    @MiguelSantos Actually, the answer I linked to (the bottom answer) uses different images - and the author specifically addresses that the other answer is only for using the same image while theirs is for using different images – William Miller Jan 24 '20 at 11:27
  • 1
    Thank you. I will try proceding in this way – Miguel Santos Jan 24 '20 at 11:28