-: Hi guys, and welcome back.
In this video, we're going to learn how to draw
a stacked bar chart using matplotlib.
Over the last couple of videos,
we've explored how to integrate matplotlib
into a application that uses a database.
Now, because we've already learned how to do that,
I'm going to start moving away from that,
so that I don't have to write the same code
and queries over and over that we have been writing
through the entire course and instead,
we're going to focus simply on the matplotlib aspect itself.
So for this lecture, I've given you the data,
and you can copy that and paste it into a file
in your project.
I'm naming that file data.py in my case,
and it contains some information that we'll need
for this bar chart.
Let's get started.
What we've got in data.py, which is linked
in the resources section of this lecture
is this set of polls.
Each poll has a name, the number of men who voted
in this poll and the number of women who voted in this poll.
Normally when you're collecting vote information,
you're gonna collect more than just male or female,
you may collect more information about gender,
you may collect information about age, and where they live,
et cetera, all that demographic data.
But for this case, since we only want to show you
how to plot a stacked bar chart, this is enough information.
So here we've got the polls list
and that is what we're going to import in app.py.
So I'm gonna go ahead and do import matplotlib.pyplot as plt
and from data import polls.
And now we can go ahead and draw our stacked bar chart.
We're gonna start off with just a simple bar chart,
a normal bar chart, so we'll do poll_titles,
we need this for the axis labels,
poll zero for poll in polls.
And we need the poll men, which is poll one
for poll in polls.
Now that we've got this, we can use that
to create our bar chart.
We also need the x coordinates of each bar,
which I'm gonna create as poll x coordinates
and this is range of len of polls.
Now we can go ahead and use matplotlib
to create the bar chart with figure equal plt.figure.
The fixed size, I'm gonna set to six, six.
Then we'll create our set of axes,
which is figure.add sub plot.
And then we can do axes.bar,
and in here we'll put the x coordinates, the poll men,
and as tick label, we'll use the poll titles.
Then at the end, we can do plt.show.
Nothing new here, we've essentially grouped everything
that we know about matplotlib so far,
and we've used it to create our bar chart.
So let me go ahead and run this file
and you can see that we get our bar chart here.
Of course, we have the problem where the poll titles
are not rotated and so forth, but we already know
how to rotate them.
So instead of focusing on that,
we're going to stack our bar charts.
The first thing we need is to get the women voters,
so we'll do poll_women poll two for poll in polls
and then inside the same set of axes,
we're gonna draw another bar chart.
So we'll say axes.bar, and again, x coordinates,
the poll women, the tick label is going to be the same,
which is poll titles, and we're gonna add one more thing,
but before we add that thing, let me just run this
as it is right now.
And you can see that what we get here
is something that looks like a stacked bar chart, right?
But these three here in the middle are not stacked,
so why not?
What we're drawing here is we're drawing the men first
and then the women in front if you will,
if we are the camera, looking at this chart,
the women are in front and they are hiding
some of the men votes.
So the total of this chart or what it goes up to
is 60 because that is the higher of the two votes.
However, for example, in the second poll,
we only see orange because the women voted more.
The 15 is down here, hidden behind it,
so this is not a stacked bar chart,
this is a pretty useless bar chart, if anything,
so we do have to make a small change
to put one on top of the other instead of in front
of the other.
And what we'll do is very straightforward,
bottom equal poll men.
So notice that poll_men is the values of the men votes,
and what we're seeing in here when we're plotting
the women votes, is that they should appear
on top of the men votes.
Essentially, what matplotlib is doing behind the scenes,
is it's adding to the women votes, the men votes,
and then it appears on top.
So if we run this, you'll see that that is what we get.
Now, let's learn how to adjust the x tick labels.
We've learned how to do that earlier on,
but we are going to do something slightly different,
because you can see that both our plots
have a tick_label property.
And what happens when we do this is that the latest one
is the one that shows up in the axes
and in this case, they're both the same
so we don't really mind which one shows up
but in other situations we might.
So it's a bit confusing to pass the tick label
into both bar polls.
And also, it's a bit wasteful
because we have that duplication.
So instead, what we can do is we can get rid of this
and that and instead use plt.xticks to pass in
the x coordinates, the labels as well
as any other configuration property.
So we'll pass in here poll x coordinates,
we need this to map each label to one bar,
then we'll pass in the titles.
It's important that these two are the same length
because the first label will be mapped
to the first coordinate and the second label
to the second coordinate and so on.
Then something else that we can do
is pass in the rotation
and the horizontal alignment as well.
Now let me run this again and you'll see that we get
our bar chart like that.
You might see that the bar chart is cut off
at the bottom though, because that's where the subplot's
adjust comes into play.
Let's give our x axis ticks a bit more room with that.
Here we can do figure.subplots adjust
bottom equals 0.35.
And now when we run this, we've got a bit of room
for our labels there to show up.
Alright, that was a bit of new information,
with drawing the bar chart and also some review
of old information.
Hopefully everything's coming together nicely for you guys.
Thank you for joining me in this video.
Thanks for watching, and I'll see you in the next one.