Visualizing Global Population

Create and customize a pie chart using population data, then save the figure as an image file.

Discover how to create visually compelling pie charts to illustrate population shares of United Nations Security Council member countries. Learn techniques to customize chart appearance and save visuals for professional presentations.

Key Insights

  • Use pie charts effectively to represent percentage shares, as demonstrated by visualizing China's dominant 70% population share among United Nations Security Council members compared to France, Russia, the UK, and the US.
  • Customize pie charts by adjusting settings, such as the explode values for slice separation, start angles for improved readability, and auto-percentage labels to highlight precise data points.
  • Save visualizations using Matplotlib's plt.savefig method, organizing images efficiently within project directories for easy access and reuse.

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.

Next up, pie charts. Last kind of chart we're looking at and then we're going to do our bar chart race and then we are done. So making a pie chart.

Pie charts are best for showing several items percent share of a hundred percent of some total. So you might have these populations of these five countries, they add up to billions, but we don't care about that, we care about the percent, the share of each one. If you took five countries, China, France, Russia, UK, and US, China would be 70% of that total still, despite adding up the four other countries.

And then the big, big chunk of piece of pie would be China. We're going to make a pie chart showing the population shares of the five permanent members of the United States, the United Nations Security Council, those being China, France, Russia, UK, US. So first of all, we are going to make a DF, we'll say UN Security Council DF, and that's going to equal the POP PIV DF.

And we're just going to slice and dice right there, those five. We'll print the shape and the whole thing. And there you go, there's your 18 year samples for your five countries.

Now, the pie chart is not a time series thing, we just want to take a snapshot. We'll do the population for 2020 only. So really what we want to do is just get 2020, which is the last item.

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.

So make a DF just for 2020. As we only want one year's worth of POPs, or pie chart, right, we want one value for China, one value for the United States, etc. We'll say POP 2020 DF, or UN SEC Council 2020 DF.

We don't even really need a DF, what we need is just the values. We just want 2020 POP list. That's all we want the United States Security Council's 2020 population as a list.

We just want to go to the last row and extract it and listify it. We're going to say UN SEC Council DF dot, we could say LOC or ILOC, we'll say ILOC. What rows do we want? We want the last row at negative one.

And what columns do we want? We want every column. And what do we want to do with that? That'll be a series, but nicer would be to listify it. So run it.

There you go, there's your five countries, populations. Okay, we took the the data frame, ILOC, in an integer location, give me the last row at index negative one, comma, give me every column, and then listify the whole thing. So it's not a series coming back.

Because otherwise, if you don't listify it, it'll work, but it'll give you a series, which you could use, but list. So what do you need for your list? You need countries, and you need populations, a label and a value, right? For a pie chart, what's the size of the pie chart? What's the name of the pie? What's the size of the pie slice? What's the name of the pie slice? Let's go up here and say countries list, because we want to feed that into our pie maker. We could have then passed that like that, right? Okay, so there's our, got our countries list up there.

We've got our pop list here. Let's just call it population list, keep it shorter, population 2020. And then we'll also print the countries, right? Just to have them together, right? There's the five countries with the corresponding populations.

That's all you need to make a pie. Pie chart wants numeric data and name data, name labels. Our numeric data is the populations, our name labels are the countries.

And we can also have colors. Let's say I want to make, want to have our own colors, and we're also going to have these other values for extruding or exploding, the explodes list. In other words, sometimes you want the pie to stick out a little bit.

You might, and the values by which it sticks out is called the, is the value of the explode property. If you've got five pies, you're saying explode or come out this far. All right.

With that, we're going to say plt.pie. We're going to feed in the numeric data, which is your populations 2020 list. You're going to feed in your countries list, which is your five members of your UN Security Council. And what doesn't it like? Can't multiply flute.

It's trying to multiply. Oh, oh, I think we have to label our data here. Labels.

There we go. And we get all this plt.show stuff there. Now we can also apply our colors.

Let's set the start angle so that the big giant China pie piece is not aiming downwards. There we go. And we're going to add our explodes as the value of explodes.

Explode. All right. So they stick out a little bit, right? That's the explosion amount, which is fine.

You can reduce those. The ones that are sticking out a lot are the 0.35s for UK and France. What if you make them 0.15, right? Tighten them up a little bit.

That might be better. Colors. Colors equals colors.

All right. And we could have percents. We'd like the percents to show up.

That would be auto PCT. Set equal to string formatting move percent 1.1 F percent percent. You might remember these from way back in the course.

These little, like for the restaurant bill calculator in lesson one. Do we want a shadow? Sure, we do. Shadow equals true.

All right. So there's the basics of making a pie chart. Now, saving images.

Anything you plot or visualize, bar charts, line charts, pie charts, they can be saved as graphics, images. So you can use them in your Microsoft Word file or something. So put them on your website.

PLT dot save fig. And we're going to go to the base path with this where we want to save to. We want to go base path into the IMG.

Let's see if we already have the file. If we do, we'll get rid of it. Yeah, we do.

OK, let's take this out and we're going to now save PLT under the name UN Security Council 2020 pop pie chart. That's the name we want to give it. Security Council member countries.

No, we don't need to make it that long. All right. Run it.

Whoa. What's the issue? Reference error. No such file.

OK, because we don't have a slash before the image. Here we go. All right.

Come back. Did it show up? Refresh. Takes a little while.

Do it again. There it is. There it is.

No, it works. There you go. It worked.

Look at that. OK, so that's pie charts and saving the images. You can do that to any of the of the visualizations we did, right? The bar charts and so on.

It's PLT dot save fig. Set the path. Don't just save it anywhere.

Set it to a specific folder in your project directory and give it a name.

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