Matplotlib Animated Bar Chart Race in Python | Data Visualization
Skills:
Data Literacy70%
Bar Chart Races are the trending animated bar plots. Usually Data Visualization tools like Tableau or Flourish is used (sometimes animation with R), but there's a very nice package `bar_chart_race` in Python based on Matplotlib making it very simpler and easier to making animated bar chart races.
Notebook: https://github.com/amrrs/youtube-r-snippets/blob/master/Python_Matplotlib_Animated_Bar_Chart_Race.ipynb
bar_chart_race package: https://github.com/dexplo/bar_chart_race
Related Videos:
Matplotlib Animation Charts in Python using Celluloid https://www.youtube.com/watch?v=3Dg80_MJSvo
What You'll Learn
Creates an animated bar chart race in Python using Matplotlib and the bar_chart_race package
Full Transcript
hey French welcome to own little coder in this video we're going to see how to easily very easily create animated bar chart races in Python it basically uses matplotlib in the backend but we are going to look at a library that wraps around the heavy lifting and then gives you a very small simple API for you to do that scene so first of all if you are not familiar with this thing so this is one of the trending plots that you get to see a lot these days on internet on social media so you get to see these bar charts moving over a period of time for you to understand how things have changed over a period of time and this is exactly what we are going to build in this tutorial or learn to build in this tutorial and the package that we are going to use is called bar chart trace so first thing is you have to install that bar chart raised bar underscore underscore trace once you are done with the thing so please import on find us and the police import bar chart trees so you have got these two libraries required for you to you know start with and then now we need to date assets so the main thing for this particular analysis is that the way you have your data set or prepared is the most important thing before even you could make this animation so this format of data set is called wide wide format where you have got all these values as columns and rather than usually low rows so this is the most important aspect so you need to have whenever you want to make any bar chart arrays for using this particular package so you need to make sure that your index value is the date which increments are in unique fashions over 8 9 10 11 12 and all other columns that you have got except that index all other columns that you have got should be the values or the bar values bar labels and the values inside it is corresponding time period value so this is how you have to create your dataset you can use Python now inbuilt pivot table or anything like that or reshape to do that this bar chart raised the developer has given you helper functions where you can see prepare you on date and prepare by data so these functions could be used to do that same thing but yes so the main thing is before even you attempt to make a bar chart race you have to have your data in this particular shape for for you to make using this particular particular liability so let's go ahead and then pick one data set that is available here so we are not going to do the reshape part we are not going to do the pivot part here in this tutorial we are going to use a data set that has been already given by the developer so this is an urban population data frame so we'll take PD Reed underscore CSV and then we are reading a CSV so let's do D F dot head and then see how the rate is it is so you can see the dataset has all these columns all these countries and then the first column is here but if you remember what we need is we actually need the index column we need the index value to be the time well you not as a column so that is that is an important thing that you have to notice that the index should be the time variable or over which the frame should be incrementing so what we are going to do is we are going to say DF is equal to set index and what do you want here is your index so once you sit here now let's do the F dot head and then you can see that now your year must be index and then you have got all other columns so you've got a lot of columns so that's the thing right so you have got columns throughout a lot of columns and you do not want to have all these columns so ideally in this plot so we will actually look at different variations of making the plot so the most simplest way you can actually make the plotters first BC at the alias' in which you call the library and bar chart race and DF is your data frame in this case my DF is called also as DF and file name if you want it to be saved in a file as an mp4 then you have to give a file name but if you do not want it to be saved as a file an mp4 but you want it to be in embedded within your notebook you don't have to give anything so keep that in mind if you want it to be a name before you want to share it on social media or YouTube or somewhere you have to give an mp4 file format but if you don't want to do any of those things you just have to ignore this so this is one important thing and then the other most important thing is you have to give the title so in this case I am going to say population population growth yeah population growth so let's see what it builds so first of all I'm not actually making any filter or anything so I've just used two arguments the first argument is the first argument is the data frame and the second argument is title which is actually required for me to make this plot and then let's see how is it going to do so it's a very heavy file very heavy data frame so it's going to take a lot of time for it into the plot so while it is doing we can have a look at the documentation so you can actually see that this this particular function it takes a lot of parameters so you can see things like title is the most important thing and then you can also load it the graphic density and you can also play with the figure size and you can also set bars like this where it's going to trace the growth so you can do all those things with this so I would request you to pay some attention to these parameters so that you can have some idea about what are these parameters trying to say and how can you customize your bar chart rays rather than you know something that looks so very normal yeah so the other important thing is the argument that you see steps per period so this is the argument that makes your animation either faster or slower so it's more like the number of frames per second the the usual term that you usually hear about this fps so it's more like that so it will help you either increase the speed or decrease the speed so as you can see now our plot is ready and you can see all the countries are there which which makes this plot ugly so this plot is if this animation is not going to go viral on social media because it looks ugly because we have not tried to set any limit for the number of bars or anything like there but you can actually see the entire point the point is you prepare the data sit in the right format once you have the data sit in the right format all you have to do is give the data frame and then you know give that title so what we are going to do now is or we'll just keep it at us just for the posterior disc a sake and then what we are going to say is we are going to say number of bars I want the number of bars is equal to let's say I want only 10 bars I don't want more than 10 bars and also in this case I'm going to say that I want the orientation to be vertical so this is horizontal orientation you can see so let's see how our vertical orientation would look like so now that is taking that's going to take a lot of time again once again but I think it should take lesser time than how much it took because you have reduced the number of bars so the number of plot making time would reduce and then the number of rendering time would also reduce so while it is reducing we can also have a look at further parameters so the parameter that says fixed said max is equal to true again to set the maximum value stick there as the top and also you can see that this is a different type lot so what we made here is slightly different you can see and what is available here is slightly different plot where you don't see things changing and that is because of this argument called fixed order is equal to 2 so when you make fixed order is equal to 2 the order doesn't change so only the size of the bar increases so that you have an understanding about its like or you see the same country in the same place or same label in the same place but only the the size of the bar changes rather than the position of the bar so this is how the vertical thing looks like so let's let's just do few more changes so one of the things that I wanted to highlight in this video is this plot that that gets plotted is basically a matplotlib plot so there are two things that you can actually do because of that or to two things that you have in your advantage one is you can specify the color map that is compatible with matplotlib second you can pass on arguments especially for the bar so whatever arguments that you usually use in a bar plot you can use those arguments in here so we are will just see so now you can see that this entire plot okay I am going to make it horizontal because it looks very ugly to me at least um and just first change we can use fix third order is equal to true okay we'll use fix or order is equal to true and then we are also going to say it find a color palette that would that would be different from what we have used so far so doc I think dog tools or by default it is the color palette that is being used so what we can do is we'll try with let's say prism let's see how the pre something looks like see map is equal to Chris um and let's see oh let's see how this new bar looks like so this this is the way you you can actually customize the bar short other way so anyone who uses this library would have the same type of bar plot or a bar chart race which you do not want to do it because then people would understand how you have terminated and then it doesn't look any any more unique right so the whole point this thing sells on social media is because it is unique so let's let's have a look at how the new bar plot comes so you can you can select from all these colors map see maps and that can help you use a different set of colors and you can also you could have seen the warning here you can also make sure that the color is not repeated okay so this color map looks actually not so good but the one advantage is that it has all these colors in a sorted order so it looks like a palette so I think that's the main point the other important thing that you often notice the bar that you have got here the the gray color bar that's moving along with so you can define it like this you can say that a period summary and then based on that period summary you can give that particular value over here so if you want that then you can use that but if you do not want that you I mean if you feel that it is a distraction for your plot then you can just leave it as it is also you can see how the the customization of this legend happens or of every frame how the change happens you can see that this mean if spring used to just show what kind of period summary you want so you can see the top value and then the leader name and also the value or a number of friends hated by that player so I think that is that is the main thing that I wanted to cover in this video which is to say that it's quite easy to make there are a lot of proprietary tools so people use tableau for this kind of purpose on Gigi animate is again quite a simpler in are to make this kind of bar chart raised on if you actually dig into the development version of this package so currently what we have installed is the version 1.0 from pi PI but if you actually get into the developer development version you would also see that the author is working on a plot B based version and also a line chart so bar chart instead of bar chart is a line chart array so you can you can have a look at those things on in case if you are interested in I hope this video was helpful to you and if you have any question please let me know in the comment section or if you have any feedback please let me know in the comment section I would link this repo so as usual we usually tank so this is from a company called under data so thanks to redditor and the developer Ted Petro for making this available for free forests so that we can use it and make cool matplotlib based animated bar chart raise thank you so much take care bye
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from 1littlecoder · 1littlecoder · 46 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
▶
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to create your Free Data Science Blog on Github with Fastpages from Fastai
1littlecoder
Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
1littlecoder
Create your first Data Science Web App using R Shiny
1littlecoder
How to create a Reproducible Example in R using reprex
1littlecoder
No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
1littlecoder
Scrape HTML Table using rvest and Process them for insights using tidyverse in R
1littlecoder
Google Teachable Machine Learning Build No Code AI solution
1littlecoder
Create meaningful fake tidy datasets in R using fakir [#rstats Package]
1littlecoder
How to enable using R Programming with Visual Studio VS Code
1littlecoder
Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
1littlecoder
Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
1littlecoder
Intro to Google Colab - How to use Colab
1littlecoder
Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
1littlecoder
Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
1littlecoder
Do you want a career in Data Science - Tamil Webinar
1littlecoder
Android Smartphone Analysis in R [Live Coding Screencast]
1littlecoder
Programmatically create Images, Memes, Watermarks using Python with imgmaker
1littlecoder
Kaggle Walkthrough to get you started with Data Science - Webinar
1littlecoder
Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
1littlecoder
Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
1littlecoder
Excel to R - Pivot + Bar Chart in Excel & R using tidyverse [Live Coding]
1littlecoder
Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
1littlecoder
5 websites to get Free Real-World Datasets for Data Science/ML Projects
1littlecoder
Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
1littlecoder
Correlation-alternative PPS (Predictive Power Score) Python Package Demo
1littlecoder
Automated Website Screenshots in R using {webshot}
1littlecoder
Installing Custom RStudio Theme (Synthwave85)
1littlecoder
Analyse Google Trends Search Data in R using {gtrendsR}
1littlecoder
3 Tips to ask question on Stack Overflow the right way to get answers
1littlecoder
Learn Data Science with R - Mini Projects - Web Scraping Zomato
1littlecoder
Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
1littlecoder
GET Hackernews Front Page Results using REST API in R
1littlecoder
Quickly deploy ML WebApps from Google Colab using ngrok
1littlecoder
Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
1littlecoder
Plotly Interactive Plots as Pandas Plotting Backend df.plot()
1littlecoder
Stack Overflow Developer Survey 2020 Highlights for New Programmers
1littlecoder
Matplotlib Animation Charts in Python using Celluloid
1littlecoder
Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
1littlecoder
Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
1littlecoder
Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
1littlecoder
Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
1littlecoder
Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
1littlecoder
ggplot2 Text Customization with ggtext | Data Visualization in R
1littlecoder
Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
1littlecoder
YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
1littlecoder
Matplotlib Animated Bar Chart Race in Python | Data Visualization
1littlecoder
Simple Python GUI Development using {guietta}
1littlecoder
#8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
1littlecoder
Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
1littlecoder
Github New Feature Profile Summary/Mini-Resume - Profile Views
1littlecoder
Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
1littlecoder
What is OpenAI GPT-3 - Hype, Examples, Worries
1littlecoder
Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
1littlecoder
Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
1littlecoder
GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
1littlecoder
How to use Advanced Google Search to extract Email Ids from Linkedin
1littlecoder
Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
1littlecoder
Flow - R Package to visualize code logic, functions as a Flow Diagram
1littlecoder
Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
1littlecoder
Create a Pencil Sketch Portrait with Python OpenCV
1littlecoder
More on: Data Literacy
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The Nervous System of the Telco: Unlocking the Real-Time Power of the Network Element Interfaces…
Medium · Data Science
Enhanced RFM Analysis for Customer Segmentation using K-Prototypes
Medium · Machine Learning
One Survey Asked Rich People Ten Times More Often Than Poor People.
Medium · Data Science
Data Cleaning Fundamentals: Building a Reproducible Data Cleaning Pipeline
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI