-: Hi Guys, and welcome back.
In this video, we're going to learn how to draw bar charts.
And we're going to use a polling data as a source of data.
So let's get started.
When we were drawing a pie chart,
we plotted option texts
against the sum of their votes
so that we can create a pie chart for each poll.
Now, we're going to be blocking every poll
together with the total number of votes in that poll
as a bar chart, so we have to change this query slightly
to give us the poll name and the sum of votes.
So we're going to select polls.title and the sum of votes.
But now of course, we want to select from polls,
we wanna join options.
And down here, we're not going to set a where
and we're gonna group by polls.name.
Of course, we now have to change the name of our query
to select polls and votes
and we no longer need this query up here.
So we're gonna get rid of that
and just get this one there.
Now we can go back to app.py
and see what we need to change in here,
our user input is going to change slightly.
In fact, we don't have any user input anymore.
All that we have is the creation of the graph.
We don't need the user to tell us any information.
So all that we'll do here
is we're gonna get rid of everything.
And just do charts.create bar chart
with the database.get options.
And of course, that does have to get renamed as well
get polls and votes, there we go.
That's a better function naming database.py
we'll use that here.
Once we've created the bar chart, we can do plt.show
in order to display it.
And now we can go into charts.py and add this function.
So we'll go to charts.py, and we're gonna do this
we're gonna get the poll information in here
and we're gonna figure out how to create our bar chart,
the first thing though, is to create the figure.
So we do figure=plt.figure.
Then we'll create our axes figure.addsubplots one, one, one.
Notice that here we're creating two different figures.
So it's okay to use the same coordinates for the subplot.
In fact, that's what you should do.
Because these will not overlap.
They are on different canvases all together.
Then at the end, we're gonna return figure.
And here in the middle, we're going to draw our bar chart,
which is axes.bar.
Now, what do we put in there,
we need the coordinates of each bar in the x axis.
So that's zero, one, two, three, and so on.
And we also need the heights of the bars.
So here we're gonna pass in the coordinates of each bar,
we're gonna do that with range of Len of polls,
that is going to give us essentially,
I'm sorry, that should be polls there,
that that is going to give us an iterable
that contains the numbers between zero
and the length of polls.
So it's gonna go, if we have 10 polls for example,
from zero to nine, giving us 10 different coordinates
zero, one, two, three, four,
five, six, seven, eight, nine.
So this is the first argument to the bar method.
The second one is the heights of each column.
So we're going to do poll one for poll in polls.
And that is going to give us the heights.
Okay, this is actually probably enough.
So let's go back to app.py and run this.
We actually made a small mistake here.
We don't need a argument in here anymore.
So I did forget to get rid of this.
We don't need any of that anymore.
So, sorry about that,
let's go back and run it again.
And another small mistake here
back into database.py we need to group by title
since that is the name of our column.
Running that again,
we get now our polls
notice that each poll has a different number of votes in it.
And this is a distribution of our votes.
This is cool, but we can't see which poll the votes are for.
So let's fix that by going over to charts.py,
we're going to add a couple of things here.
I'm going to start off by closing
the project on the left though,
since we don't really need that.
In order to display a label,
we need to pass in another argument tick label.
And this has to contain a string for each column.
So we'll do poll zero for poll, in polls
and that is going to give us one string
for each column that we've created.
Now rerunning this is going to give us
the poll titles there,
but as you can see, not terribly easy to see,
especially because they are long.
So there's a few things we can do,
we can obviously extend this
until you can barely see them in this big screen,
and not ideal.
I'm sure there's something that we can do a bit better.
The usual thing to do with these labels
is to rotate them slightly
so that they'll appear, you know, like this in each side,
rather than straight and that way
because they'll all be slightly rotated,
they won't intersect with each other.
However, in order to do that,
we also need to make this area down here a little bit bigger
so that they'll have more space when they are rotated.
Otherwise they will end up clipped.
So let's learn in the next video
how to adjust this area down here,
how to adjust the size of the plot.
And then in the video after that,
we'll learn how to rotate our labels.
I'll see you in the next video.