So let's try getting Some fun stuff.
Let's start plotting some population pyramids.
So I'm going to cut half a copy half of this code and walk you through what we're doing here.
Here we go.
And kind of explain to what we're doing.
So one thing we're doing in most of our videos where I end up with a web application as a final product
is we want to kind of figure out our logic our all our Python code clean it up.
We want the minimum amount of code necessary to get to get where we want to go.
And we want to wrap that into a nice clean function or sometimes two or three functions the smallest
amount of functions possible the smallest amount of code as possible because what's in the cloud is
gonna be a lot harder to debug harder to troubleshoot.
So you know you want to make sure your your functions make sense from an object oriented perspective.
If you've done object oriented programming you want to make sure they could almost live on their own.
Create Value bring value to your program on their own.
And that's what you want to separate.
You don't want everything to be in one.
You want to separate it so that then you can maybe have some logging in there for a troubleshooting.
But you know I'm jumping the gun here.
Let's get back to plotting our first population of pyramid.
So I'm going to abstract already the values of the country and the year.
Remember we have to give two pieces of information in order to get a bar plot.
We want a country and a year.
So I'm already creating a country variable and a year variable here I'm setting my country variable
to the United States of America and the year to 1970 I just pulled those randomly.
Though you've got to make sure this is spelled exactly the same and that the year exists in the data
set of course.
And then we're going to create our subset just like we did before with Japan.
Now we are doing it but with variables right.
This is we're starting to abstract our logic into a function.
We are going to sort the result write a by by the age groups and we are then going to create a new subset.
So I'm I'm creating a y index I'll show you how that works in a second but we are creating two subsets
so this is a subset already.
Right.
This is a subset of the main dataset and now we're creating two subsets from this subset.
One for male one for female we know we don't have exact same amount of rows because they come from the
same table but we need to separate them because when we're good at this bar plot we're going to create
is actually two bar plus one next to the other.
Right.
So we need to have the males in one two females in one.
We need them separated and then we've got to create a max scale because I want to know which is the
longest the largest amount of population either on a female side or on a male side.
Some countries have more males some countries have more females it's interesting we'll look at that
in a second but we want to get the largest of either because that's going to be the largest.
That's how large we're gonna make both graphs if it's going gonna be the the size and actually let's
run this and see what is our max scale.
OK.
This is our largest population we don't know if it's on the female the male side for United States in
nineteen seventy in the thousands right.
So.
OK and now let's start plotting.
So let me pull all the plotting code here and paste it here.
I mean walk you through a lot of things going on here.
Because appalling is really the main product that we're going to yield right.
Our web application going to yield is going to yield this population plot.
So we want you know we want to spend a little time there because it's a lot of things going on.
So we're going to create a plot it's one plot but it can have subplots so two subplots within that plot
and they're gonna be in column so one column one column one one for female one for male.
We're going to share the y axis right.
Those are going to be the age groups and figure size 10 by eight this is perfect for a Jupiter notebook.
We are going to create the background so we the color you add the background color can be beige in this
case and this is something that's going to work in the on the Jupiter notebooks will not work on a website
which is fine and we're going to create a figure text a title.
So the title is important because we we want to know both what it is a country and a year.
So obviously we're going to do a country a position the point five point nine a positions country.
The title is that a country column the year a font size you can make this big or smaller.
And we want it to be centered.
Then we get to create.
We have two axes right.
We have columns two columns and a subplot so x 0 and Axis 1 actually 0 is going to be the males axis
one is gonna be the females and we're going to create a bar plus was Bar H.
Why is we Why.
Which is basically the age groups by the data set will be X male and we're going to align it in a center
and light blue for the males.
Wouldn't create a title for the males right here and we're gonna to create the largest we're gonna make
sure we're going to fix it to the largest number of the large population whether it is female or male
for whatever Asians as a matter we're going to fix both sides to be you know set to the maximum population
then we're gonna work on axes one which is the same thing another other Bar H but this time for female
align center color will be pink title will be females we're also going to set it to the largest of the
of the scale of the Whatever we find unless it grid so true that you can turn it on off depending if
you think it's too busy you can remove them and we are going to set the tick size here to to be the
Tick label of the age group basically we want the labels to be this right because it makes the y axis
DCP nice and legible and we're going to invert one of the axes right because one is can be flipped the
males can be flipped and we're going to do plot show hopefully this works let's try and it didn't work
out because I didn't not reload the plot library OK that's the problem right and it means I forgot to
load one thing right here
let me run it right here
just again come on come on narratives look it up.
So here it is right.
We have to Bar H plots one is invert One is light blue one is in pink we have the correctly set the
age groups as our y axis and we have the number in thousands of the population and you can see this
is the largest population in 1970 were males between 10 14 so this is the largest population this is
our max X and that's why there is some white space here because we set this width to be the same width
here right.
That wasn't Maxim this is going to make when you have the animations you wanted to be nice always equal
both size right so you can quickly see if one country has more females one can you have more males it's
interesting to see here we can see that there are more males United States in 1970 and this age group
at 10 10 to 14 had more right.
So it's always interesting to see and then we have it we have created one chart and let's scroll down
and briefly to talk about this and we'll leave some for the next video but we need like I said right.
We're trying to abstract all that work into something very clean because we want to move all that to
the Web.
So we basically need to make sure our code works really well that it's very fast it's not going to cause
bugs.
You could add some logging if you wanted all sorts of things but now I wrapped everything we did here.
Basically these two right.
These two Jupiter notebook areas have set them into one single function and that's a function that now
write it as if it works should work right.
I mean let me grab these two again
and now instead of calling it doing it manually I can simply call do something like this I could say
United States comma 19 we'll change it up or go.
1989 1990
and now we should be able to get a plot just from calling this number that United States 1990.
Right.
So we went a few more years into the future into the future.
20 years or so.
Yeah.
Twenty years after 1970 and we see that group that used to be the 10 to 14.
Now is the 30 to the 34 are the largest group but it kind of climbs and that's very important to see
right.
That movement of the bars tends to climb upwards.
I'm going to show you a next video as we kind of explore some of this data.
I'm not sure you're an animation of Japan from the beginning to the end you'll see how interesting it
is and you'll see how the population tends to move upwards.