Instructor: Hi guys and welcome back.
In this video, we're going to learn how to create
pie charts with matplotlib and we're also going
to use our poll data to create some pie charts.
Let's get to it.
We've been using dot plot to create line graphs.
But matplotlib can very easily create pie charts as well.
In its simplest form, though,
a pie chart requires only a list of numbers.
So let's get started with that.
We're gonna do figure equal plt dot figure.
And we're gonna add our axes here.
Since we're only doing a single set of axes,
I'm just gonna do this instead of using the shorthand.
So we'll do add, subplot.
And here we can put in one, one, one,
since we are only creating one row and one column.
And the index that we want to place
this set of axes in is one.
You don't have to put this in.
This is the default.
So if you leave it empty,
that's what's gonna happen anyway.
Then we can do something like axes dot pie.
And this is gonna create a pie chart.
And what it needs is three numbers.
For example, 63, 28, and eight.
Let's say that we've got a poll from our database,
and it received 99 votes, which is what this adds up to.
So about 63% of those votes, is this,
28% of the votes is that, and 8% of the vote is this number.
This is going to create a pie chart with three segments,
where this one equates to 63% each of the total.
So let me just run this and show you what that looks like.
We get this pie chart here.
63 is the blue, 28 is the orange and eight is the green.
Notice that matplotlib has also taken care
of the colours for us and it uses the default colours
which look alright, in order to draw this pie chart.
We can also pass in labels for each.
So I'm gonna do that now.
What I'm gonna do is extract these numbers into a variable.
So I'll call it option votes, for example.
And then we'll create option names.
And we'll have Flask, Django, and It depends.
Depends like that.
And I'm gonna spread it into three lines
just so it's a bit more readable.
And now we have to pass these in here.
So the first one is option votes.
And the second one is labels equal option names.
And it's important that this is a keyword argument,
because the order of these arguments does matter.
And it's important that this is a keyword argument.
So now we can run this.
And matplotlib by default is going to separate these labels
by 10% more than the radius of the pie chart.
So these are slightly outside the pie chart there.
In the documentation, you'll find how to adjust that.
There's quite a lot more arguments that we can pass into pie
but we're gonna look at two in particular.
We're gonna look at explode.
So I'm gonna simply bring these
into multiple lines like that.
And then we'll have explode here
and I'm gonna do 0.1, zero and zero.
And what this is going to do is it's going to separate
the first segment, which is 63 or Flask,
from the centre of the pie chart,
while leaving the other two unseparated.
So if I run this, you'll see what I mean.
The centre of the pie chart is here,
and this segment has sort of navigated itself
a bit further away.
Finally, we can use auto pct to display the percentage
that each segment is out of the total within the segment.
In order to do this,
we pass in a format string surrounded by percent signs.
So we can do 1.1 f percent, percent.
And now we're gonna show this as the number of digits.
f because it's a fixed amount of decimal places,
and then a percent sign.
And the string is to be surrounded by percent signs
in order to be valid.
So really, this is the string that we're passing in
and the percent signs are just necessary in matplotlib.
And at the end of the day, what we get back
is 63.6%, 8.1%, 28.3%.
So again, the one here before the decimal place
tells us that we are dealing with numbers.
The point one f tells us how many fixed decimal places
we want to use.
In this case, it's one.
And then the percent sign at the end
is just this percent symbol there.
And it is surrounded by percent signs
to be valid in matplotlib.
Let's expand our app a bit more.
We're gonna show the user the open polls
that we've got in our database.
And we're gonna let them pick which poll they want to graph.
And we're going to create a pie chart
from the data in our database.
The first thing we're gonna need
is to start working on our app structure.
Since we're going to retrieve data
from our polling database,
we're going to build this app
like we did in our earlier sections.
So I'm going to get rid of this file,
since we're not gonna need it just now.
And I'm going to create first of all a dot env file.
And here we're gonna put
the database URI of our application.
I'll also create a dot env dot example file
for other developers in case
that we want to store this in GitHub.
So we'll do database URI equal there.
Now as well as this,
this you should fill in with your own database URI,
by the way.
I'll put mine in, in just a moment.
Next, we're gonna create a database dot py file.
And here we're gonna put our queries that we'll need
to retrieve the information from our database.
Remember, we're gonna ask the user for a poll.
They're gonna pick the poll that they want.
And we're going to create a pie chart
out of the options in that poll.
So what we need really is polls
and options with their votes.
So select polls,
is going to be something like select star from polls,
and the options, select options in poll,
is gonna be something like,
select options dot text and the sum of votes,
from options.
Join polls on options dot poll ID equal polls ID.
Join votes on options dot ID equal votes dot option ID.
Then we're gonna add a where clause,
polls ID equal whatever the user entered.
And we're gonna group by options text.
So these are the two queries we'll need.
And now we can add the rest of our database dot py.
Like importing psycho pg two, loading in the dot env,
and we'll create two functions for these queries.
So I've gone ahead and added my imports
and called load dot env.
But of course, we do need to instal psycho pg two
and python dot env.
So again, you can do this via your terminal,
by simply doing pip instal psycho pg two dash binary
and python dot env.
Make sure to instal psycho pg two dash binary.
Make sure that your pip is up to date
before running this command.
And that is going to instal psycho pg two dash binary
and python dot env.
Now, our red underlines will magically go away.
Next up, we're going to connect to psycho pg two.
So we'll do connection equal psycho pg two dot connect,
os dot environ dot get of database, URI.
And then we'll create our two functions
to execute these queries.
So we've gone ahead and do that.
We have our connection to psycho pg two here.
And then we have our two functions, get polls,
which opens up the transaction, gets a cursor
and executes the query and then returns everything.
And get options which takes in a poll ID,
which is supposed to be an integer,
and then does the same thing by executing
select options in poll, passing in the poll ID argument.
Alright, now that we've got this,
we can go ahead and open a Python console
and we're gonna test our database dot py
to make sure it works.
So the first thing to do is to do import database.
And because this Python console
is running in our current project, we can do that.
We can do database dot get polls and run that.
And you can see that we get back our polls.
So at the moment, my data is a bit bare.
We only have one poll, and three options and one vote,
but it's gonna let us see how to do this.
Alright, so we've got the data here.
This is a good way to test things out, by the way.
So now we can stop this interpreter, close that down
and go ahead and create our app dot py.
I'll start off app dot py with the prompt.
Well, I'll import database and then create to prompt.
We're gonna have a prompt for the menu, enter q to quit
or anything else to chart a new poll.
And then we'll also have one
that asks them for the poll ID they want to chart.
We're gonna have a user menu
that simply runs through over and over until they enter q.
They're gonna get the polls on the database,
and we're gonna have a function
that prompts them to select one of those polls.
In this function,
we're going to take in the polls from the database,
and then we're going to iterate through them,
printing out the ID and the poll title.
And what we're gonna do then
is ask them for the selected poll that they want to chart
and we're going to call another function with that.
So I'm going to do this.
Selected poll is int of input of the poll prompt.
And then we're gonna chart the options for the poll.
That is gonna be another function
that simply takes that, gets the options from the database,
and now goes ahead and draws the pie chart here.
So here we will draw the pie chart
with the data from our database, which is as you know,
the option text and the sum of the votes.
But in order to draw the chart,
I'm going to create another file
that I'm gonna call charts dot py.
The reason for that is because I want to decouple
user interaction in app dot py to creating
the charts in charts dot py.
Here in charts dot py,
we're gonna start off by importing matplotlib.
So we'll do import matplotlib dot pie plot as plt.
And then I'll define the create pie chart function.
Which takes in the list of option titles and their votes.
Here we create the figure plt dot figure.
And we create the axes, which is figure dot add subplot,
one, one, one.
Whenever I'm creating, you know, proper functions,
and I'm starting to develop a larger application
and so forth, I like to be more explicit.
So that's why I'm passing in one, one, one here,
rather than leaving it as the default.
But it's up to you, that is the default.
Then we'll do axes dot pie.
And what we wanna pass in here, is first of all,
the list of value.
So that is option one for option in options.
And then we wanna pass in the labels,
which are option zero for option in options.
Remember that each option comes with the text as column zero
and the sum of votes as column one.
You should have put in a comma there.
And finally, I'm gonna put an auto pct here
of percent 1.1 f, percent, percent.
And again, that is 1.1 f percent,
which is the format string surrounded by percent signs.
Then we're gonna do return figure.
And I don't want to do figure dot show or plt dot show here,
because again, I don't want this function to be responsible
for displaying things to the user.
I want that to be kept in app dot py.
So now we can come here
and say figure is equal to charts dot create pie
chart with the options.
I do have to import this, so I will do that.
And then we can do plt dot show.
And here we do need to import,
matplotlib dot pie plot as plt.
Note that here we are creating the pie chart
and assigning it to a variable,
but then we're calling plt dot show and not figure dot show.
Again that is because if we call figure dot show
that is not going to display a window.
That is going to assume that we are displaying the figure
in some other window already existing.
So we have to call plt dot show
and that is going to use the figure that was created here.
So technically, we don't actually have to assign that
to a variable if we don't want, but I think it's good form,
and it will allow us to do some more configuration later on,
or do things like save the figure to a file,
if we wanted to do that instead, a bit more easily.
Let me run this.
And I'm gonna enter here, and I'm gonna select poll one.
Oh, and we get an error here, the column is not correct.
Let me go back to database dot py,
and this should be called option text.
And similarly down here.
So let's run that again.
We can enter option one here.
And we've got ourselves another small error.
Sum of votes should be sum of votes dot option ID,
my apologies there.
And now we've got it for sure.
We can enter one there.
And here we've got our figure.
As you can see,
at the moment we only have one vote in one of the options.
So that's all that's coming back.
But as we add more,
we would get a better looking pie chart here.
Later on in this section,
we're going to learn how to save this to a file
instead of simply displaying a window
That is gonna make it a bit more useful, in case you want
to produce the output for many different polls, for example.
Or in case you want to allow
the user to save the output somewhere.
However, by showing this you can still save it
just by clicking on this little Save icon there
in the default figure viewer.
Alright, that's everything for this video.
Thank you guys for joining me.
I hope you've learned something,
and I'll see you in the next one.