Publish AI, ML & data-science insights to a global community of data professionals.

Representing 5 Features in a single animated plot using Plotly

Using a single animated Bubble Chart to analyse data and observe trends .

Photo by Wengang Zhai on Unsplash
Photo by Wengang Zhai on Unsplash

Why use Plotly?

Plotly is a visualisation library which is fast becoming popular for its amazing capabilities. What sets Plotly apart from the other visualisation libraries is its ability to provide interactive, dynamic, easy to use and highly detailed plots. Using Plotly we can interact with our plots through button clicks, drop-downs, sliders and even create animations. Whats even better is the fact that we can plot very appealing graphs with almost no effort as Plotly does all the heavy-lifting for us under the hood. We will see how easy it is to use a bubble chart and animate it with just one function call.

Analysing Trends Amongst Footballers over the past 5 years

For today’s article we will use the Fifa dataset from Kaggle which has details about footballers from 2015–2021 . Specifically we shall see how a footballer’s age, value, overall rating, transfer value and playing position are related and whether these attributes disclose any patterns over the passage of time. To extract these features from various files I had to do some pre-processing which is a present in the entire code notebook. Here is what the final data looks like after preprocessing and combining various files.

What is a Bubble Chart?

A bubble chart is essentially a scatter plot, where the data points don’t have the same sizes. The size of a bubble can be associated with an additional attribute ( usually numerical ) which can help discern the data points better. Additionally we can colour a data point according to a specific category that the point belongs to.

In summary here is what the components of the bubble chart look like

  • X and Y axes – Numerical Features
  • Size of the bubble – Numerical Feature
  • Colour of the bubbles – Categorical Feature

Adding Animations

We will be adding an animation that automatically moves a slider (which is responsible for changing the year we want to visualise) forward in time to show us the players’ attributes each year . On adjusting the slider the graph is updated to plot a player’s attributes for the given year.

Let’s Dig in !

I chose the top 20 players in 2015 (as per overall rating ) to visualise and see how they are doing in 2021. Note- I have only chosen players who have been active every year from 2015–2021.

  1. I would recommend you run the code on Google Colab since it has Plotly pre-installed. If you want to run the code locally you must pip install plotly
  2. Set the arguments for the plot. As you can see below we have used the dataset name as the first argument. The x axis is set to plot age and the y axis is set to plot overall. The size parameter is set to reflect the market value of the player. The color ** parameter is set to player_position which is categorised as Attacker, Midfielder, Defender or Goalkeeper. We also set the _hovername parameter to show the player’s name. Do not forget to set the max_size parameter since the true market values are in millions and we need to cap the bubble size. Most importantly we set the animation_frame parameter to** year to automatically add a slider for the different years under the year column of the dataset. Finally we just set a range for the x and y values and a title for the graph.
  1. All it took to create a comprehensive bubble chart depicting 5 features and an animation was only one function call ! Impressive how easy Plotly is isn’t it. Feel free to play around with the options in the top right corner of the plot for utilities like zooming, selecting specific areas, or downloading the graph as an image. Also notice how the player’s information is displayed when we hover over a bubble!

Looking for Trends

  • Firstly we can see how age relates with market value. The value of a player increases and peaks around his late 20s and starts declining once the player enters the 30s. This decline is the sharpest for Attackers depicted by the blue bubble and the most gentle for ** Goalkeepers depicted by red bubbles**. The bubble size helps us visualise the market value and compare players based on it in a very intuitive way.
  • We can also see how overall starts declining with increasing age. This makes sense as footballers have short careers. Again it is very interesting to see that Attackers and Midfielders decline faster compared to Defenders and Goalkeepers since most of their attributes rely on physique, stamina and pace which starts wearing off after playing for so long.
  • One very obvious correlation is that between overall and market value. However it is interesting to see how age also starts affecting market value since clubs would want to buy players who can play for long periods.

Animation Caveats In Plotly

Animations are designed to work well when each row of input is present across all animation frames, and when categorical values mapped to symbol, colour and facet are constant across frames. Animations may be misleading or inconsistent if these constraints are not met. – The Official Plotly Documentation

Conclusion

This was just a glimpse of Plotly’s powers and when paired along with Dash, Plotly can create beautiful animated Dashboards and Graphs which have a high degree of interaction. I also wanted to point out of how Plotly provides buttons and tools (for zooming, selecting etc.) on its own, these can be great utilities during deeper analysis and can make visualisation much easier.

I hope this was interesting and fun. See you next time!

Check out my github for some other projects and the entire code. You can contact me here. Thank you for your time!


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles