-: Hi guys and welcome back.
In this video, we're going to start learning
about matplotlib and we're going
to draw a simple line chart.
So let's get started.
To draw any graph or chart,
we first need to create a figure.
And inside a figure, we can draw graphs or charts.
In matplotlib, these graphs or charts are called plots.
And plots are drawn on top of axes,
the plural for axis.
Most plots are drawn on x- and y-axes,
although some plots can be drawn
on three-dimensional axes as well.
We're gonna start by importing pyplot.
So I'll do import matplotlib.pylot as plt.
This is the standard import for matplotlib.
Then to create a figure, we just do plt.figure.
You can see that PyCharm is complaining
that I have an unvalid import here
because I haven't installed matplotlib yet,
so let's do that.
You can do this over in your terminal.
Make sure that you have activated your virtual environment
and then simply do pip instal matplotlib.
And if this fails, you should upgrade pip first.
But here you can see it passes
and matplotlib is now installed.
So the red line goes away.
Now, after creating a figure,
we can go ahead and plot inside that figure.
So we can do plt.plot, one, two, three, four, for example,
and three, five, nine, and 25.
Now, running this, which I'm gonna do
by right clicking and pressing on run
is not going to do anything
because we haven't told matplotlib
to display the output produced.
We can do that with plt.show.
Now we can run this file again.
I'm gonna click the green play icon at the top right
and you can see that we get
our matplotlib graph viewer here.
A lot of this is not stuff
that we've developed ourselves.
We've just told matplotlib to draw this graph
but it comes with matplotlib
and it allows you to do things
like pan around the axes, for example,
and zoom in and out
and do some configuration as well.
Plt.plot is used to plot any arbitrary data.
Is creates a 2D set of axes,
and by default, it produces a line graph
where these points are the x-coordinates
and these points are the y-coordinates.
And then it joins the points with a straight line.
We can change it so that instead of joining the point
with a straight line,
it uses circles for the points let's say.
We can do that with a third argument.
I'm putting in the string o.
If we do this, you'll see that we get circles now
instead of a joined line.
There's also a list of built-in colours
that we can use to colour the circles or lines
or anything else automatically.
Here I'm using ko as the string
to colour these circles black.
K is the letter for the colour black
because B is for blue.
We can also get rid of that
and assign the result of plt.plot to a variable,
and that gives us the plot itself.
The line or the points that we've drawn.
Then we can do plt.setp
and here we can pass in the plot,
which is lines in our case,
and then we can also change certain parameters.
Here I'm gonna pass in colour,
making use of the American spelling,
and we can pass in a hexadecimal colour in there if we want
and that allows us to use a custom colour if we prefer.
As well as coordinates in the x-axis,
we can use categorical variables.
So here I'm going to make a couple of changes
and instead of one, two, three and four in the x-axis,
we're gonna plot, for example, Men, Women and Children.
And then we can plot values against that.
Naturally, these values will normally be coming
from our database or some sort of file
but here I'm just plotting arbitrary values
but you can see that now we get these labels
in the x-axis instead of the numbers.
Matplotlib will automatically create even ticks,
these things here are called ticks,
if you rpovide numerical data.
But if you provide categorical data,
as we're doing here,
then it will only show the ticks provided.
Of course, it can't interpolate between men and women
to give you a tick in the middle.
As well as all this,
which allows us to create the plot
so we can also do a lot more.
For example, provide labels for the x and y-axis
or provide a title for the plot itself.
We can do that with plt.xlabel
and this allows us to define, for example,
a label for the x-axis.
Plt.ylabel allows us to define a label for the y-axis.
I'm gonna set it to Amount here
and plt.title allows us to define a title
that appears above the chart.
I'll run this to show you what it looks like
and you can see that we've got our labels here
at the bottom and at the left
and our title at the top.
All right, that's everything for this video.
Thank you guys for joining me,
I'll see you in the next one.