Back to Blog
Make a scatter plot matplotlib5/5/2023 ![]() ![]() ![]() ![]() 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. 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. ![]()
0 Comments
Read More
Leave a Reply. |