To create a donut chart all we have to do is add a bit more code to this cell here. 1
So I'm going to copy it, paste it below, so I've got our original pie chart that I'm going to leave as it is. 2
And here I'm going to add my additional code. 3
The code I'm going to add is drawing a circle onto our chart, a white circle that is. So I'll create a variable 4
called "center_circle" which is going to hold on to my circle and use matplotlib to 5
create a circle, "plt.Circle", with a capital C, open parentheses, 6
and now we have to supply some arguments. 7
Let me hit Shift+Tab on the Circle to take a look at the quick documentation. 8
So our Circle requires one input, one input is mandatory to create it and it basically needs to know 9
where to center the circle. 10
It needs an x and the y coordinate in these parentheses like so. 11
So we're gonna give it a tuple of these coordinates. 12
I'm just gonna center it at 13
0, 0 and that's it. 14
Let's add the Circle to our figure. 15
So I'll say "plt.gca().add_artist(center_circle)". 16
Now let me hit Shift+Enter and what we get is something like this. 17
What's going on? 18
Well, our circle is probably too big, it's not sized correctly, so we'll have to supply a radius. 19
I'm going to make this circle a lot smaller, 20
make it 0.6 for the radius and hit Shift+Enter. 21
Much better, right? 22
We've got the circle now sitting in the middle of our pie chart. 23
The only problem is is that this circle is blue and it looks looks very, very odd. 24
So let's make it white. 25
The way we can do that is by supplying this color as an argument to a parameter called "fc", "fc = 26
'white' 27
will set the color of the circle to white. 28
Now we've got something that looks a bit more like this. 29
This is starting to look a lot better, right? 30
The only thing is our offset doesn't quite work anymore. 31
It makes it look very, very strange. 32
So I'm going to delete the explode argument here on our pie chart and refresh our graph. 33
There we go. 34
Not bad, but I do want to move these percent labels. Back up here on our pie chart, 35
we can do that with an argument called "percent distance", "pctdistance", all in lower case and if I set 36
that to 0.8, then my percent labels start moving out and they're more or less centered now 37
in their segment of the donut chart. 38
Now I'm fairly happy with this design. 39
Let's review the code a little bit. 40
We created a circle using matplotlib, so "plt. 41
Circle()" is our equivalent of "pd.DataFrame()" and then we've supplied a couple of inputs. 42
We've supplied a color, we've supplied a size of the circle and we've supplied where that circle should 43
be drawn. At 0, 0 44
it's completely centered. Then what we did is we had to add this circle to our figure. 45
So with "plt.gca" we get the current axes, so we're getting the current axes and we're adding the circle 46
with the "add_artist" method to the axes. 47
So that adds the circle to our figure, which means it gets drawn on top of our pie chart. 48
Okay, so we've almost mastered the donut chart. 49
The only thing that I really want to show you is how to draw a donut chart with more than two categories, 50
because oftentimes these donut charts really come into the fore when you have three, four or five categories, 51
then they start looking really, really great in all sorts of reports. 52
So just as an exercise, let's create a donut chart with four categories. 53
Copy this cell, paste it below, so I'm keeping the original and working off this one and I'm going to add 54
a few more category names, so say "Updates" and "Promotions". 55
Now since we have four categories, I'm going to have to update my list of sizes as well. 56
And I'm just gonna make up these numbers here for the amount of spam, legitimate emails, updates and promotions. 57
So say we have 25 spam emails and I don't know 43 legitimate emails, 19 updates and 22 promotions. 58
So we've got that. 59
Now it's just a matter of picking the colors. We can keep the red and blue, but we have to add two more 60
colors that fit well with this palette. 61
Going back to our American Palette from Flat UI colors, I think I'm going to go for the light green and 62
maybe the sour lemon here, 63
the yellow. 64
So I'm going to copy this, update our list here, making sure that the hex code of the colors and single 65
quotes and I'm going to copy the sour lemon for the promotions. 66
There we go. 67
Now I've got my four categories. 68
Let me hit Shift+Enter and see what this looks like at the moment. 69
Not bad, right? 70
But what if we wanted to have a small gap between each of these categories? 71
What if we wanted to explode the donut chart the way we've exploded our pie chart? And the easiest 72
way to do this is to create another list of values, say "offset = []" and then maybe 73
0.05, 0.05, 0.05,... 74
I'll make the offset equal between all of the, all of the categories, 0.05 as well. 75
Then for the pie chart, we'll just say "explode = offset". 76
Now let me hit Shift+Enter and look at our results. 77
Brilliant. 78
And that's really all there is to it. 79
I think pie charts are, they're simple, they're clear, they're easy to code up, but you can also make them 80
look very, very good with the right choice of colors and a little thought on the formatting. Having done 81
some cute visualizations in the past lessons, 82
it's now time to return to do some hardcore programming. In the next couple of lessons, 83
we're going to be looking at natural language processing. 84
We can't just feed raw e-mail bodies into our machine learning algorithm. 85
We need to prepare these e-mails for our Bayes' classifier to work. 86
I'd say this step probably falls a bit more into the data cleaning part of the project, but it will equip 87
you with some very, very powerful tools and techniques to tackle all kinds of text data that you're gonna 88
be encountering on your personal projects as well. 89
Natural Language Processing is a huge topic and it's super lucrative and super interesting. 90
So I hope you're as excited as I am for the next section. 91
I'll see you in the next lessons. 92
Take care.