Creating Population Bar Chart Races with Python

Create an animated bar chart race visualization using Python's bar_chart_race library.

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.

All right, 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 or when you're in Colab, it's exclamation pip. Like you would run this in a terminal to install this if you're 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 `pop_piv_df`.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

When you make a bar chart race, you have to use the columns as your racing bars. Hence the `pop_piv_df` where we have the countries as columns. And there's a million things you’ve got to feed into this. It's a giant method—`bcr.bar_chart_race` is the method—and 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’s a copy-paste massive thing. We're going to first set the data frame. The `df` property is the first thing—it's the `pop_piv_df`.

That's where the data is, comma. Filename is going to be the name of—it's actually the file path. Where do you want the resulting video to go? It's going to be a video. What do you want to call it? We want our video to be called pop top 30, which we're now going to delete from the video folder and make a new one.

So pop top 30 countries—maybe “Bar Chart Race: Top 30 Countries by Population 1955 to 2020”—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—the 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? 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—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 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 the bar size is 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 separating them.

The period label and period summary will create this little summary on the side. There are 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, 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 the number, the finer the quality. 250 is pretty high.

Then there's this thing called the C-map, which is a color map. If you Google it—Matplotlib C-map—you'll see there's all kinds of color maps, as they're called. These are 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 above your animation? Not the file's name, 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=''`, 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’ve got to be pretty small.

We're going to say five, which is pixels, because there are a lot. Those are the countries. Actually, the ticks are the country.

We're going to say `bar_label_size` and `tick_label_size`, both of them five. Small little country names on these little bars.

Then we're going to set a font dictionary—`shared_fontdict`. A font dictionary contains 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 unless you 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 kwargs—keyword arguments—fine. `alpha = 0.7` is a little bit of transparency. That means the bars, when they pass each other, will 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. Cue Gladiator: Are you not entertained? All right.

Okay. Thank you very much. We're out.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram