

#SCATTER PLOT MATPLOTLIB EXAMPLE CODE#
I won’t go through everything individually, but I’ll share the commented code below for some of the essential changes – titles, colours, comments, etc. Great work! We can now see who all the points are! Improving our chartĬlearly there is plenty to improve on this chart. The ax.add_artist function then draws this on our plot. For each row of our data we create a new variable called ‘ab’ which uses the AnnotationBbox function from matplotlib to take the desired image and assign its x/y location. We then iterate through our dataframe with df.iterrows(). What’s happening here? Firstly, we have created our scatter plot with white points to hide them against the background, rather than interfere with the club logos. fig, ax = plt.subplots(figsize=(6, 4), dpi=120)Īx.scatter(df, df, color=‘white’)Īb = AnnotationBbox(getImage(row), (row, row), frameon=False) Let’s do this by creating a new plot, just as before, then iterating on our dataframe to plot each team crest. This function prepares the image, but we still need to plot them. Our badges are likely to overlap, in case you want to make them transparent, change this figure to any number between 0 and 1. The images are too big by default.05 reduces their size to 5% of the original. In this case, it will look in the path that we created in the dataframe earlier. We use the plt.imread function to read in an image from the location that we provide. Return OffsetImage(plt.imread(path), zoom=.05, alpha = 1)

Let’s add this to a function as we’ll use it a few times: def getImage(path): The first one we will use is ‘OffsetImage’, which creates a box with an image, allows us to edit the image and readies it to be added to our plot. Adding these requires a couple of extra matplotlib tools. Our base figure provides the canvas for the club badges. Adding badges will hopefully add more value and information to our plot.

Super simple chart, and without annotations or visual cues we cannot tell who any of the points are. We have covered scatter plots before here, so let’s get straight into it. Once we have this, we can get fancy with our badges and other cosmetic changes. This gives us the correct dimensions of the plot, the axes and other benefits of working with a matplotlib figure in Python.

Let’s save this in a new column called ‘path’: df = df + ‘.png’īefore making our plot with the badges, we need to create a regular scatter plot. As we took the time to match the badge file names against the team names, this is really simple – we just add ‘images/‘ before and ‘.png’ after the team name. We have our numbers to plot, but we need to add a reference for each team’s badge location in a new column. Let’s import our modules, data and check the first few lines of the dataframe: import pandas as pdįrom matplotlib.offsetbox import OffsetImage, AnnotationBbox To start with, our data has three columns: team name, xG for and xG against. All of this is already prepared for you in the Github folder. The team names match up to the data that we are going to use soon. The simplest way to do this is to keep them all in a folder alongside our code and have a naming convention of ‘team name’.png. To automate plotting each image, we need to have some order to our image locations and names.
