2

Wanted 'Age' as the x-axis, 'Pos' as the y-axis and labels as 'Player' Names. But for some reason, not able to do label the points.

Code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import adjustText as at



data = pd.read_excel("path to  the file")
fig, ax = plt.subplots()
fig.set_size_inches(7,3)

df = pd.DataFrame(data, columns = ['Player', 'Pos', 'Age'])

df.plot.scatter(x='Age',
                y='Pos',
                c='DarkBlue', xticks=([15,20,25,30,35,40]))

y = df.Player
texts = []
for i, txt in enumerate(y):
    plt.text()
at.adjust_text(texts, arrowprops=dict(arrowstyle="simple, head_width=0.25, tail_width=0.05", color='black', lw=0.5, alpha=0.5))

plt.show()

Summary of the data :

df.head()

             Player Pos  Age
0  Thibaut Courtois  GK   28
1     Karim Benzema  FW   32
2      Sergio Ramos  DF   34
3    Raphael Varane  DF   27
4       Luka Modric  MF   35

Error :

ConversionError: Failed to convert value(s) to axis units: 'GK'

This is the plot so far; not able to label these points: This is the plot so far; not able to label these points

EDIT: This is what I wanted but of all points: This is what I wanted, but of all points

Also, Could anyone help me in re-ordering the labels on the yaxis. Like, I wanted FW,MF,DF,GK as my order but the plot is in MF,DF,FW,GK.

Thanks.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • what do you mean with "label these points"? Can you indicate how the result should look like? | You may want to create a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with data, so that people can actually run your code – max Nov 16 '20 at 07:22
  • You can reorder the axis labels by reordering the list in which they are kept. So, step one is to create a list for each of the three columns - this will make it a lot easier for you to begin making use of (and manipulate) the data. Do this and then revisit the question I helped you with earlier - if you're still stuck after that, let us know. – ChaddRobertson Nov 16 '20 at 12:21

1 Answers1

4

A similar solution was described here. Essentially, you want to annotate the points in your scatter plot.

I have stripped your code. Note that you need to plot the data with matplotlib (and not with pandas): df = pd.DataFrame(data, columns = ['Player', 'Pos', 'Age']). In this way, you can use the annotation()-method.

import matplotlib.pyplot as plt
import pandas as pd

# build data
data = [
['Thibaut Courtois', 'GK', 28],
['Karim Benzema', 'FW', 32],
['Sergio Ramos','DF', 34],
['Raphael Varane', 'DF', 27],
['Luka Modric', 'MF', 35],
]
# create pandas DataFrame
df = pd.DataFrame(data, columns = ['Player', 'Pos', 'Age'])


# open figure + axis
fig, ax = plt.subplots()
# plot
ax.scatter(x=df['Age'],y=df['Pos'],c='DarkBlue')
# set labels
ax.set_xlabel('Age')
ax.set_ylabel('Pos')

# annotate points in axis
for idx, row in df.iterrows():
    ax.annotate(row['Player'], (row['Age'], row['Pos']) )
# force matplotlib to draw the graph
plt.show()

This is what you'll get as output:

picture

max
  • 3,915
  • 2
  • 9
  • 25
  • 1
    any idea how to make the labels not overlap with each other? –  Nov 18 '20 at 09:32
  • 1
    If you want that the labels do not overlap their corresponding points, use the `textcoords='offset points'` option. Have a look at the [docs](https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.annotate.html). The [advanced annotation](https://matplotlib.org/3.1.0/tutorials/text/annotations.html#plotting-guide-annotation) help is also quite good. I am not sure, if there is an option if your points are very close and labels would overlap each other. You then may want to work with arrows on a fraction of the labels – max Nov 19 '20 at 06:48