# Matplotlib Tutorial (Part 3): Pie Charts

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=MPiz50TsyF0
- **Дата:** 12.06.2019
- **Длительность:** 17:02
- **Просмотры:** 174,508
- **Источник:** https://ekstraktznaniy.ru/video/11843

## Описание

In this video, we will be learning how to create pie charts in Matplotlib.

This video is sponsored by Brilliant. Go to https://brilliant.org/cms to sign up for free. Be one of the first 200 people to sign up with this link and get 20% off your premium subscription.

In this Python Programming video, we will be learning how to create pie charts in Matplotlib. Pie charts are great for visualizing your data in a way that clearly shows the proportions of each category. We'll learn how to plot a pie chart, customize how it looks, and more. Let's get started...

The code from this video (with added logging) can be found at:
http://bit.ly/Matplotlib-03

✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C

## Транскрипт

### Intro []

hey there how's it going everybody in this video we're going to be learning how to create pie charts and pie charts are pretty simple to use they're similar to bar charts that we looked at in the last video but instead of plotting certain values for each category it's instead going to show us the proportion that each category contributes to a whole so basically just like the name implies you can think of our chart as a pie and each category will be a slice of that pie and the larger the slice means that category has more of whatever values were measuring so let's go ahead and jump into this and see some examples but real quick I'd also like to mention that we do have a sponsor for this series of videos and that is brilliant org so I'd really like to thank brilliant for sponsoring this series and it would be great if you all can check them out using the link in the description section below and support the sponsors and I'll talk more about their services in just a bit so with that said let's go ahead and get started so first I'm going to go over a very basic example to see how this works and then we'll look at some more real world data where we plot popular programming languages using a pie chart so I have a little bit of starting code here that you might recognize if you're continuing from the previous videos but if you're not then let me quickly go over the starting code that we have here so I'm simply importing pipe lot from matplotlib we are setting the style to be 538 just so that this looks a little nicer and currently all we have is a title of my awesome pie chart and we are making the tight layout which will get some default padding in there that looks nice and then we are doing plot dot show which will actually show our plot for us and I've also got some colors commented out down here but we don't have to worry about that right now this code will be available for download I have a link in the description section below that takes you to my github so if you want to follow along then you can download this code and follow along okay so like I

### Pie Chart Basics [1:47]

said pie charts are pretty simple and straightforward so first of all we need the values that we want to represent in our pie chart and again we can think of these as slices of the pie so for example let's start off easy and say I wanted to just represent two numbers in a pie chart so I'm gonna say slices is equal to and this will be a list so I'll do a list of 60 and 40 and that's actually all we need to create a very basic pie chart so to plot that I can just say PLT dot pie not plot sorry PLT dot pie and we want to make a pie chart of those slices so if I run that then we can see that it plots out our values of 60 and 40 and the 60 value takes up 60% of our plot and the 40 value takes up 40% of the plot but this isn't very useful right now since we don't have any labels or any indication of what we're plotting so let's add some of that so I'm going to go back to the code here

### Labels [2:44]

okay so we have our slices but let's also add labels for our slices so that we know what's what when we look at our chart so to do this we can just add labels as a list and these will correspond to the slices that we want them to be a label for so underneath the slices here if I was to say labels is equal to and I'll just pass in a list here so the first one I want to label as 60 the second 40 and then we can add those labels to our pie chart simply by saying labels is equal to that labels variable that we just created and also just a quick side note our values actually don't need to add up to a hundred percent they can be anything and the pie chart will calculate their proportions accordingly so if I were to double these values then we'd still get a chart that looks the same so if I was to double this to 120 and double this to 80 then those are still going to take up the same proportions of the pie chart so if I run that then we can see that our 120 here takes up 60% and that's labeled 60 and our 80 here still takes up 40% and that's labeled 40 now I don't know about you but I like to have some separators on the edges here of these colors instead of these colors just bumping up against each other so to do this we can pass in some wedge properties so back in our code after labels this is going to

### Wedge [4:06]

be in the pie method we can say wedge props for wedge properties and this is going to be a dictionary of values and we just want to change the edge color so I'm going to type in edge color and let's just set that to black so if I run that then now we can see that we have a black edge when for these were just a solid color here now if you're wondering how I knew to change the edge color property there for that wedge property that's something that I looked up in the documentation and if you google matplotlib wedge then it'll take you to their documentation page with all of the customizations that you can make okay so lastly let's look at how we can specify our own colors for these slices and after we do that then we'll take a look at a more real-world example where we create a plot for popular programming languages so to change the colors of these slices we can do the same thing that we did with the labels and just create a new list corresponding with the colors that we want for each slice so I'll just add a few extra colors here and also let me

### Slices [5:11]

add a few extra slices so I'm going to pass in 30 and 20 here and then I will also pass in labels for these so just extra one and extra two now these aren't going to take up sixty-forty anymore but that's okay we're just doing a demonstration here and now we want to pass in a list of corresponding colors so I'll pass in let's see blue red it's also do yellow and we'll do green as the last color there and now we can pass those colors into our chart by passing those in to the pie method here so right after the labels I'll pass sanic colors is equal to and we called that list colors so I'll save that and now if we run that then we can see that we have more slices here all of these are labeled and we have some custom colors now I almost never use just the color names like we did here because I think that looks kind of like something you'd see in an elementary school textbook or something like that I just think it's kind of bright so you also have options of using hex color values to select your colors as well and I almost always use hex values to get the exact color that I want or I'll let matplotlib styles choose default colors for me but that's why I have these colors commented out down here in my code because I'd like to

### Colors [6:29]

use those for this example instead so instead if I were to copy all of these here let me grab all of these and I'm just going to paste these in as my colors so now we have that blue color this is a blue hex hex red color a hex yellow and a hex green and I just found these on a color palette online that I thought looked good so if I run this then we can see that now those colors are a bit more muted and I think that looks a lot more professional than using those built-in named colors okay so now that we've gone over the basics of pie charts now let's look at a real world example and plot popular programming languages now I hope they have these available here in a

### Popular Programming Languages [7:12]

snippets file that I have open they are let me delete those color comments right now so that we can see everything so in my snippets here and I'm gonna have this available in the description section below as well if you want to go along with these snippets but I'm gonna copy and paste all of these and then I'll explain what this data is and I'm just gonna paste this over top of our custom colors here and also our slices and labels so if I save that now that we don't have those custom colors I'm also going to have to remove colors from the pie method here since we're no longer using that and now let me explain this data here so I grab this data from the 20-19 stackoverflow developer survey and these slices here represent how many respondents said that they use a particular language and the labels here are the language that they said that they knew so the first value of 59,000 here is how many people said they knew JavaScript and 55,000 HTML and CSS and so on now we've got a lot of languages here these are the top 15 there were around 30 languages here before we narrowed this down a bit so this is one mistake that I see sometimes when people are creating pie charts don't really look good when you have this much data they're really best for comparing you know five things or something like that so let's see what this looks like if we run this with all 15 of these values so we can see here that this just gets extremely crowded and it might take us some time to find out exactly what we are looking for and it's also hard to get a good comparison from language to language like if we were to try to compare SQL to JavaScript here then it looks like JavaScript has a larger chunk here but it's really hard to tell by how much because there's so much data so if you wanted to plot this many items then I would definitely recommend using a bar chart like we used in the last video instead of a pie chart I wouldn't use a pie chart unless you have you know if you have more than five items but if you have less than five items then I think a part by chart would work fine so with that said let's strip down our data to only include the top five programming languages so I'm gonna go

### Deleting Clutter [9:25]

back to our code here and now I'm just going to delete all of these up until the fifth item so one two three four five so Java is the fifth and then I will delete all the way up until the fifth item here so 1 2 3 4 5 ok so now if we run this then we can see that looks a lot better than all of that clutter that we had before ok so now let's see a few more things that we can do with this pie chart so a lot of the times you'll see some pie charts that have an emphasis on one piece of the chart so for example let's say that I that we were preparing a talk for a Python conference and we wanted to emphasize the Python slice on this chart so to do this we can pass in an explode argument and this will be a list of values that will offset the slice so to do this I'm going to create a an explode

### Explode [10:16]

list here underneath our labels we'll set that to an empty list ok and these values will represent will be a list of floats which represent how much we want to emphasize that slice so a zero will keep that slice how it is and any number besides zero will represent the fraction of the radius that we'd like to explode the value now that might not make sense right now but let's go ahead and take a look at an example here and I think it'll make more sense so if we want to explode Python that is the fourth item of this list so I'm just going to say 0 0 and now on Python I'll just say zero point one and that will put 0 for Java as well and now we can add explode here to our arguments so I'm going to say explode is equal to that explode list okay so now if I run this then we can see here that Python is now exploded out here a little bit and we have some emphasis on a Python here now that point one that we passed in to that explode whenever I said that represents how far it is from the radius that point one just means that it is ten percent of the radius exploded out here if we were to do like a point five then that would be really big and that would be half of the radius that it is exploded out so I'll do a point five and run that and we can see that now it's exploded out by half of the radius the point here is that half but I think that's way too large so let's go back to point one I think point one looks pretty good so let me close both of those down okay so there are a

### Other arguments [11:57]

few more arguments that we can pass into our PI method that you might find useful so let's take a look at some of these now one of these is just for aesthetics but we can add a shadow to our chart by passing in an argument of shadow equals true so right after explode here I'll just say shadow equals true and if we run that then we can see that now this looks a little 3d here we have a little shadow along the edges along our slice that's exploded out here just kind of makes it pop out a little bit now another argument that we can pass in is the starting angle and if you want to change exactly how the chart is rotated then you can play around with this so for example if I was to say start angle is equal to let's say 90 degrees so I will save that and run it and now we can see that this chart was rotated a little bit I think it picks an arbitrary angle to plot at first but when we set this equal to 90 we can see that now our first value of JavaScript here starts at a 90-degree angle here and then this goes all the way around to the last item in our list here which is Java so if we really wanted to emphasize Python then we could play around with that angle and get it to show up exactly how we like so that's what start angle does okay and

### Percentages [13:16]

lastly if we want to actually display the percentage that slice is taking up in our pie chart then we can add in an argument for that as well now this syntax is going to look a little strange I always need to look this up in the documentation when I do this but to do this we can say let me actually go on a new line here so to do this we can say Auto PCT so Auto push percent is equal to and now we're passing in a format string here so this is going to be a % one point one f percent whoops percent % and again I know that syntax looks a little bit strange there but that is the format string for how we specify a percentage so if I save that and run it whoops and I forgot my comma there so we had a syntax error if I save that and run it then now we can see that we have the percentages here within our pie chart telling us how big of a piece of the pie each of these values takes up so that's pretty much the basics of a pie chart they're really nice for just getting a quick overview of the data like this and what values take up what percentage of the whole but like I said if you have more values than 5 then I would recommend using a horizontal bar chart or any or a vertical bar chart depending on your data but the pie chart is definitely an option that you have okay so we're just about finished up here but before we end I'd like to also mention the sponsor of this video and that is brilliant org so in this series we've

### Outro [14:47]

been learning about matplotlib and how to plot data in python and brilliant would be an excellent way to supplement what you learn here with their hands-on courses they have some excellent courses covering the fundamentals of statistics and these lessons do a deep dive on how to think about and analyze data correctly they even use Python in their statistics courses and will quiz you on how to correctly analyze the data within the language they're guided lessons will challenge you but you also have the ability to get hints or even solutions if you need them it's really tailored towards understanding the material they've also recently released a programming with Python course and they even have a coding environment built into their website so that you can run code directly in the browser and that is a great way to complement watching my tutorials because you can apply what you've learned in there of problem-solving environment and that helps to solidify that knowledge so to support my channel and learn more about brilliant you can go to brilliant org Forge / CMS to sign up for free and also the first 200 people that go to that link will get 20% off the annual premium subscription and you can find that link in the description section below again that's brilliant dot org forge slash C m/s okay so I think that pretty much covers it for pie charts I hope you feel like you got a good understanding of when you might use these and also how to customize these a bit to visualize your data exactly how you'd like and in the next video we're going to be covering stacked plots and some people call these area charts it's very similar to a pie chart but instead of looking at how things are proportioned at a single time stack plots will allow us to see these different proportions over time so definitely be sure to check that out but if anyone has any questions about what we covered in this video then feel free to ask in the comments section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think can find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching
