Create engaging, animated bar chart races to visualize dynamic data transitions clearly and effectively. Learn to customize key animation parameters, ensuring your visualizations communicate compelling stories.
Key Insights
- Install and import the Python module "bar_chart_race" to easily create animated bar charts, using "pip install" and giving it an alias for convenience.
- Customize animation settings, including orientation ("horizontal"), sorting order (descending), number of bars (30), and animation smoothness (60 steps per period with a duration of 5,000 milliseconds per period) to enhance visualization clarity.
- Adjust visual presentation parameters such as bar spacing, color mapping ("Viridis"), transparency (alpha value at 0.7), and label font sizes to improve readability and visual appeal.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Alrighty, crowning glory, animated bar chart race. This is it. This is that thing you saw racing at the beginning.
This guy, we want to make that right now. So to do that we need to install the module. We need to install the bar chart race module.
To use pip install, pip python install packages uses an exclamation point to install and it is the same install move you'd run in the terminal, but when you're in VS Code doing or when you're in Colab doing it, it's exclamation pip. Like you would run this in a terminal to install this if you want, if you're working not in Colab or not in a Jupyter notebook. Bar chart race with underscores is the name.
We're installing it, doing all kinds of stuff, it worked. Once it's installed, we want to import it. Import bar chart race as bcr, right? Give it an alias.
Good, in business. Now all data is all prepped with columns. So our data frame, our data frame that we're going to be using for this is the poppiv df.
When you make a bar chart race, you have to use the columns as your racing bars. Hence the poppiv where we have the countries as columns and there's a million things you got to feed into this. It's a giant method bcr.barchartrace is the method and you're going to feed in dozens of, you're going to set dozens of properties, some of which I set for us already, but some we need to type.
I can't just say here copy pastes, massive thing. We're going to first set the data frame. The df property is the first thing, it's the piv, poppiv df.
That's where the data is, comma. File name is going to be the name of, it's also the, it's actually the file path. Where do you want the value, the resulting video to, it's going to be a video, where do you want it to go and where, you know, what do you want to call it? We want, we want our video to be called pop top 30, which we're going to now go in and delete out of the video folder, make a new one.
So pop top 30 countries, maybe bar chart race, top 30 countries by population 1955 to 2020 bar chart race, just so it's a totally different name. That's all, it's a really long name. I make long file names just because they're specific.
Anything less, you're missing information. Maybe take out the by. Top 30 countries pop, top 30 pop countries, 1950 to 2020 bar chart race.
Anything less, you're taking out information. That's kind of important. All right, next orientation, how do you want this bar thing to go? We want it sideways, horizontal, that would be an h, right? Sideways bars.
Next, how do you want to sort the data? In other words, what bars do you want at the top? Sort in descending order. In other words, China, most populous nation up at the top in descending order. How many bars do you want? That would be 30, 30 countries.
Do you want a fixed order for these bars? Like they never move? Absolutely not, because that would defeat the whole purpose. We want them to change position whenever one country overtakes another in the population count. Would you like to keep the max fixed though? So as they change order, keep it to 30 the whole time.
That would be correct. So yes. How many steps per period? Now a period is every epoch in time.
So 1955 is one period, 1960 is another period. How many steps do you want per period? In other words, how many frames in the resulting video do you want? If you don't have enough steps, it's kind of choppy, just like animation that doesn't have enough frames. We're going to go for 60 steps per period.
So it'll be 60 frames of animation to show every transition from one state to the next, one year sample to the next. When you go from 1980 to 1985, it's going to take 60 frames to do that. Now how long do you want those 60 frames to take? Different matter altogether.
We would like that to take, say, 5,000 milliseconds. If it's going to take five seconds to run 60 frames, you're looking at 12 frames a second, which is choppy compared to 24 frames a second for feature films. But it's still pretty good for this kind of thing.
We're just going to roll out. We're going to just roll here. I haven't Googled every single one of these.
Interpolate, period, false. Interpolation typically refers to blending from one state to the next, so it's not really blending, it's just jumping. Do we want to label the bars? Absolutely.
We want to know what the bars are, what bar size relative to the full. So every bar is going to be allocated a certain amount of size. There's 30 bars, the bars are going to be kind of skinny.
Do you want the bars to touch or would you like a little gap between each bar? I think we want a little gap. We'll say 0.9, so the bar will only occupy 90% of its little alley. It's kind of like bowling alleys with gutters or something, like separating them.
The period label and period summary will create this little summary on the side. There's too many settings to talk about. I mean, this is normal programming procedure for some stuff like this.
You just grab and go. Like you're driving your car, you're not really worried about, you know, when I press the gas, you know, what's happening. Fuel is going into the fuel injectors.
No, you're just driving. We're driving the car, okay, sometimes. So that's the fig size.
Then we're going to say, how many dots per inch do we want? That's DPI. The higher number is for finer quality. 250 is pretty high.
Then there's all this thing called the C-map, which is a color map. If you google it, mat, font, lib, C-map, you'll see there's all kinds of color maps, as they're called. These color schemes and standards.
We're using Viridis, which is kind of the default one. Feel free to change it. Do you want a title or above your, you know, animation? Not the file's name, right, obviously, but the title up at the top.
Absolutely. Right, we've got our little title up here. We're going to use the default size.
So for the title size, we're not going to set it. We'll say title size equals empty string, which means we're not changing anything, just letting the defaults come through. The size, what about the size of the bar labels? Well, the bar labels are population, and they got to be pretty small.
We're going to say five, which is pixels, because there's a lot. Those are the countries. The country, actually, the ticks are the country.
We're going to say bar label size and bar and tick label size. All of them five, both of them five. Small little country names on these little bars.
Then we're going to set a font dictionary, shared font dict, a font dictionary is key value pairs. And in this case, we're just setting the color to 0.1 on the font, which doesn't necessarily mean too much if you don't Google it. And again, you've got dozens of these things.
You don't necessarily Google every one. I've gotten through most of them. I've explained most of them.
Let's just get this thing. Flapping. I mean, get this bird flapping.
Bar, wargs, keyword arguments, fine. Alpha 7.7 is a little bit transparency. That means the bars, when they pass each other, they'll be a little bit see-through.
And lastly, filter column, colors, true, close, all this stuff. You need all this jazz. Okay.
Take a nice long look. Here's the top half. Count to three.
Pause. Get it working. Get it right.
You don't know it's working until you run it. It's a lot of stuff. Okay.
Run. Whoa, already. Okay.
I need a comma after that. Run. Need a comma after that one.
It's going to take a while. It'll take a minute or so. It's going to sit and spin a while.
Okay. Done, I guess. Let's find out.
Boom. Well, let's just download it. There you go.
That's the wrong one. That's the one. Okay.
There it is. All right. It's working like a charm.
That is the Capstone final project, final exercise for this course. Hope you enjoyed it. Q Gladiator, are you not entertained? All right.
Okay. Thank you very much. We're out.