![add point to scatter plot matplotlib add point to scatter plot matplotlib](https://i.stack.imgur.com/3pArS.png)
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):
![add point to scatter plot matplotlib add point to scatter plot matplotlib](https://i.stack.imgur.com/3ivF6.png)
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.
![add point to scatter plot matplotlib add point to scatter plot matplotlib](https://www.tutorialgateway.org/wp-content/uploads/Python-matplotlib-Scatter-Plot-8.png)
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.
#Add point to scatter plot matplotlib code#
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. Plot badges on top of the scatter pointsĪll the data and images needed to follow this tutorial are available here.To do this, we’re going to go through the following steps: In this tutorial, we’re going to create a scatter plot of teams xG & xGA, but with club logos representing each one. Key data points can be highlighted with annotations, but when we have a smaller dataset and value in distinguishing each point, we might want to add images instead of anonymous points. They can show huge amounts of data, but often at a cost of being able to tell the identity of any given data point. Scatter plots are the go-to for illustrating the relationship between two variables.