All right, so in the upcoming lessons I want to talk a little bit more about data visualization. In particular, 1
I want to to show you guys how to create some beautiful pie charts. 2
And afterwards I wanna show you how to create a variation on the pie chart called the doughnut chart. 3
Now, both of these types of charts are very, very common, especially in business reports, 4
so it's very important that we also get these two charts under our belt. 5
The first thing I'll do of course is add another markdown cell and I'll call this "Number of Spam Messages 6
Visualised ( 7
Pie Charts)". There we go. 8
Now before we dive into the visualizations, we should probably check how many spam and how many non-spam 9
messages there are. For this, our dataframe's 10
value_counts method is going to be very, very handy. 11
So if we take our dataframe and select the CATEGORY column, we can use value_counts and then 12
parentheses to get a breakdown of how many messages of each category we've got. 13
Here we can see that we've got 3900 non-spam or ham messages and we've got 14
1896 spam messages. 15
Now, since we're gonna be using these numbers later on, let's store them in some variables. 16
So I'll say "amount_of_ 17
spam = data. 18
CATEGORY.value_counts()[ 19
1]". 20
And the amount of ham is gonna be equal to the same thing except it's going to be followed by "[0]". 21
Zero because I access the first value in this series and one to access the second value 22
in this series. 23
Now, to do our visualization, 24
we're gonna be using our old friend matplotlib. 25
So let's import matplotlib's pyplot functionality as plt at the top of our notebook. Going to our 26
notebook imports we'll say "import 27
matplotlib.pyplot as plt". 28
Now, as usual, we're also gonna be adding 29
"%matplotlib inline". 30
This line of code here is Jupyter notebook specific 31
and it allows us to export our images for our charts when we export the notebook. 32
So if you ever go to Download, Download notebook as Notebook, then if you have this line of code in here 33
then you get the graphics as well. 34
All right. 35
So how do you create one of these friendly pie charts? 36
The first thing I'll do is I'll create a list of our category names, so category_names 37
is gonna be equal to a list with the square brackets, 38
"['Spam', 'Legit Mail']" and the next thing I'll do is create 39
another list. 40
I'm going to call this one "sizes" for the size of the different pieces of the pie. 41
This is going to hold on to the amount_of_spam and it's going to hold on to the amount_of_ham. 42
Now we can use these two to generate our chart, "plt.pie(sizes, " and then the labels 43
of the chart are gonna be equal to the category_names. With "plt.show()" we can see what our basic, 44
very, very basic pie chart is going to look like. Now, 45
I don't recall if I actually pressed Shift+Enter a minute earlier on my notebook cell, but let me hit 46
Shift+Enter now and we'll quickly find out. 47
Nope, get my "NameError: 'plt' is not defined". 48
This means that we have to go back up, 49
or I do at least, and hit Shift+Enter on my imports. 50
There we go. 51
Now I can come down here and actually generate my pie chart and it looks like this. It's pretty ugly actually. 52
So we're gonna have to customize this and make it look presentable. 53
We're going to dress it up. 54
The first thing I'm going to do is actually make those font sizes on this thing look a bit larger, so I can access 55
the font sizes of my labels here with the property called "textprops"; 56
"textprops = { 57
'fontsize': }", say, I don't know, maybe 6, 58
see what happens. 59
OK, they get even smaller, 60
not what I want. 61
Maybe I'll pick 16. 62
There we go. 63
That's a bit better, but this whole thing is still very, very unconvincing. 64
For starters I can see that it's not very, very sharp on my screen. 65
So what I can do in this case is manipulate the figure itself, so "plt. 66
figure(figsize = )", 67
I don't know, say, "2,2", right. 68
Changing the figure size. 69
See what this looks like. 70
It gets a lot smaller, but this isn't the only thing I can change on the figure size. 71
If I wanted to make this whole thing look tack sharp on my screen, then I can actually set the density 72
of the pixels per inch or DPI to whatever my monitor supports. 73
So I've got a pretty decent monitor that I'm working with here. 74
It has quite a high resolution, so it actually supports 227 pixels per inch. 75
And this will have an interesting effect, so if I press Shift+Enter now, you can see that the whole thing 76
starts looking first of all a lot larger because it's scaled up and second of all the edges start looking 77
a bit more clear. 78
Only thing is my font size is probably a bit too large now, so if I go back to font size say 6, 79
then it starts looking a bit better like this. 80
Now, when you're working on this project, have a play with the different figure sizes, the DPI values and 81
the font size and see how it affects your scaling. 82
You're probably gonna have to use a bit of trial and error to get this thing looking good or looking 83
the way you want it to on your monitor. 84
If you ever want to export this thing and you know save it to, put it into a report or what have you, 85
it's probably better to create a larger version of this figure and then just right click and save the 86
image as a larger image, because then if you ever need to include it into a report or into a Word document 87
or whatever you're using, then you can downscale it and it'll look a lot better than taking a small image and 88
scaling it up. 89
Now let me pull up the quick documentation on the "pie" functionality from matplotlib. 90
We can actually see that there's quite a few different parameters that we can set. 91
Let me show you two of them. 92
I want to show you the start angle which is currently set to none and I want to show you this auto percent 93
parameter that we've got here and how to use it. 94
Let's have a play with the start angle first. 95
So if I set the start angle to zero, let's see what we have. 96
We can see that the chart looks like so, then we can see that this blue portion just rotated by 10 degrees 97
from this position. 98
You can see this rotation actually very clearly if I start going to 20, 30, 40, 50 and so on. The blue piece 99
starts rotating counterclockwise from the x axis, 100
so from a straight line that goes from the center out to here. 101
If I set the start angle to 90, then our chart rotates by exactly one fourth of the way around counterclockwise. 102
All right. 103
So that's the start angle. 104
Now let's have a look at the other property that I wanted to show you - 105
auto percent, 106
and we're gonna set that equal to '%1.1f%%' and 107
what we get now is we get a pie chart where the percent of the chart is displayed on the chart itself. 108
The formatting is to one decimal point, because we've put 1.1 here. 109
If instead I had "1.2f", then it would show me two decimal points. 110
So depending on the amount of precision that you want on your pie chart you can choose the formatting 111
of the percent sign as appropriate. 112
Now, 113
personally, I don't like showing too many digits after the decimal point. In most use cases, 114
you usually want to favor readability over precision when it comes to these visualizations anyhow. 115
If we wanted to round this to the nearest percentage, we can put a zero here and then we'll get 33 and 116
67. 117
Now I find this chart here, 118
size wise and formatting wise, is starting to look pretty decent. 119
The only thing that I don't like, 120
are these really awful colors that it's showing me here by default. 121
So let's, let's change this and try to improve the design a little bit. 122
Just so we have a record of our old chart, 123
I'm going to copy this cell and I'm going to paste it below. 124
Now I've got two different pie charts and I'm going to spice up the design on this one and leave the 125
other one as it is. 126
One of my go to places for picking a nice palette is a website called Flat UI Colors. 127
Let's check out this American Palette here that they've got. The first color that I'm gonna grab is gonna 128
be maybe Pink Glamour, means I can go back here into my Jupyter notebook and save my colors as a list. 129
I'm going to call it a "custom_colors = ['']", paste. This is gonna be my first 130
color, comma, back to the American Palette and I'm going to select the color that goes well with this 131
other category. 132
So I think something that would be contrast-y would be this blue one here. 133
So I'm gonna take this one, go back here, 134
add that as well. Having added my colors' hex codes to a list, I can now feed them in as an argument to 135
my "pie" method. 136
So, "colors =custom_colours" and Shift+Enter will color in my pie chart in a much more beautiful way 137
and I've picked some light colors, so that the dark text, this 33 and 67 percent is still very, very readable. 138
Now one thing we can also do to make the design a little bit snappier is to break out this spam section 139
here from the legitimate e-mail section, so we can actually have a little bit of a gap in between the 140
two sections and we can do this by supplying an argument here called "explode", so "explode = " 141
and then we also supply a list. 142
This list will have two values, say 0 and 0.1. 143
These numerical values here set the size of the gap. 144
So let me press Shift+Enter and show you what this would look like. So passing in an argument of 0 145
and 0.1 will get the chart looking like this. 146
If we had 0.5 instead, then the gap would start looking a lot bigger. 147
So I'm going to set this back to 0.1 and I can also meddle with the first one here, so 0.5 148
instead of 0 and we get again something like this. The offset that we're applying here, 149
if we go to the quick documentation essentially specifies the fraction of the radius with which to offset 150
each wedge. Since we have two wedges, the red one and the blue one, we're supplying two numbers. So in my 151
case, I'm offsetting one of them by 0.1. 152
If I offset both of them by 0.1, then the gap between the two wedges doubles. 153
Now I quite like the look of a small offset, 0.1 will do for me. 154
In the next lesson I'll show you how to take these designs a little further and make a donut chart. I'll 155
see you there.