Welcome back to our class of all, of course, about the complete introduction to data science with
Python.
So in this class, we are going to talk about matplotlib and learn how to create a basic matplotlib
graph or a chart and modify this graph or chart.
So basically, this is one of the best ways that you guys can use to visualize data inside of Python.
So to be able to do all this, a divided discourse and fourth by four parts.
So basically the first part we are going to learn to import matplotlib.
So basically works pretty much the same way as Banda's, no worse Kibuye.
So things that we talked about in the past few classes, then we are going to create a really simple
graph, really create something basic so you guys will see what it all looks like.
Then I'll show you a more advanced way to be able to write everything down.
So basically how usually we write down function.
So this way we are able to create not only one line inside of our graph will not only integrate one
function of our graph, but have like four or five, six functions inside of our graph.
So basically more than one X in one way.
So you can have an infinity of Xs and OS, and then after that we are going to add titles, labels and
everything.
And finally, we are going to change the style of our graph and make it more beautiful.
So we are going to talk about all this inside of this class.
So let's start.
So basically, the first thing that we are going to do is pretty simple.
We want to import matplotlib to be able to work with this Python library work package.
And how exactly do we import it?
It's pretty simple.
So we will write down import and want to import as Matt Plup Lib Dot Bayport.
So we want to import all this as VLT.
So basically field you can write down anything that you want, but Kielty is the usual, the usual name
of the pipeline.
All right.
So if you don't write this, if you don't write down this line of code, normally nothing would work
because you didn't import matplotlib.
So when everything is ready, what we will do right now, we are going to create a basic graph with
just one function inside of it.
So basically it's going to be one line.
So we'll have one X and one white.
So let's write it down.
So pretty simple.
The first thing that will do right on field.
So we are making a reference to matplotlib by writing down Kielty and the function that we are going
to use will be blood.
So basically Bluck right here and then we are going to write Xs here and our Y's here.
So for the Xs, let's say it's going to be one, three and five.
And for the guys, let's say it's going to be five, ten and twenty five.
So basically we have just two numbers.
If you guys want to add more, add more numbers.
Let's say five.
We have 10 in here.
Let's say it's going to be one hundred and twenty five.
All right.
So basically, we have our numbers right here.
But if we click on this little thing, it's not going to work because we didn't wrote down the function
that will allow us to print everything right here.
So to be able to see this graph, what we'll need to use is the show function.
So it's going to be Kielty that show.
And we want to see this what exactly we have just created.
So pretty simple.
You just write down to that show and you will be able to see the graph that you guys have just created.
So basically we can compare to what we see right here.
So we have one in here is going to be one four five.
So with five is right here, then we'll have three.
So three will be somewhere right here for ten.
Then we'll have five for twenty five.
So basically five would be somewhere here.
So four twenty five makes sense.
And finally ten for one hundred and twenty five which makes sense as well.
So can see this is the graph that we have just created.
So if we put our cursor right here, you can see down there everything is fine.
So basically this is the first grab that we have created.
All right.
Right now, let's say that you guys want to have more than one line.
So if for those who don't understand what I'm saying, so this would be one function.
So this is just one function.
Let's say you guys want to have more than one function.
How exactly you can do all this?
So it's pretty simple.
There is another way to write it down.
Usually we don't write that this way.
So basically, this is just a presentation way.
So just to present one chart or one graph.
So the way that we are going to write everything down will be pretty simple.
So we need our X's and our way.
So basically X
right there and are wise.
So let's say we have to function.
So it's going to be X and X one, Y one.
So we'll have X to and Y to.
So let's write it down, so if we're X1, what we'll do right now, we'll just add something.
So let's say, for example, we want to add what we want.
I will give them numbers.
So it's going to be, let's say, two, four and eight and four.
Why no one is going to be for eight and twenty four, for example, for the X number two is going to
be pretty much the same thing.
So let's say it's going to be three, six, nine and four y number two is going to be in this case,
let's say 10, 15 and 30.
Let's keep it like this so you can see the first thing that we will do usually will be write down our
will want to write down.
So basically all our database will be here.
So all the X values and the Y values, then next thing would be writing down the plot, that plot function,
for example, to the plot.
And then what we want to do is show everything so that plot will have X and Y and find in the second
one will be plot that blood X to Y two.
So basically the the plot function will simply be the function that will allow us to work with that,
let's say, for example, X and Y.
So basically we want to work with X1 and Y one and we'll just write down X right here and wait right
here.
Same thing right there from the work with X2 and Y two.
I'll just write down X2 and wait two.
And finally, the last thing that we want to do is just show everything.
So we'll write down that show and we can run our up.
So basically what's going to happen is that we'll see are two lines right here so we can compare if
it's what we have written.
So this is exactly what we have written.
So basically we have our first line right here.
So four X one and Y one.
It's blue and four x two, Y two.
It's right there.
If I change, for example, my thing right here instead of X to Y two, I just write down X and Y two.
You'll see it's going to be a bit different.
Because you can see became parallel because I'm working with two exes that are the same.
So once again, this is more in mathematics, but as you can see, it works pretty well to be able to
generate a graph.
All right.
So right now that we have our function, next thing that we want to do is write down, give a title
to our graph.
And not only this, we want to give a value to X and Y value to Y.
So basically a title for X and it's a little too wide.
So how exactly do we write all this down?
It's pretty simple.
So we'll just write down that title.
So this will allow us to generate a title.
So let's call it out on the list.
Let's call it desk.
So the title of our graph will be Test and then X values.
We'll call it.
Let's call it test X.
So here we'll have built that X label.
So basically we are giving a name to X labels, to the X, to the X value.
So basically we'll call it, let's call it, as I said, test X values and plotty dot Y labels, which
would be the name of the white labels would be test y values.
So test y values.
All right.
So what I have just done right here, I just give a name to my access.
So basically this would be the name of my axis and this would be the title of my chart.
So everything what I did right now is pretty simple.
So I still have the same, the same functions right here.
So my functions are still the same.
I just added a title and I added a axis name.
So if I run everything, as you can see here, I have my chart name.
So basically test, then I have text a test y values, which is my wife values and test X values, which
are all my X values.
So you can see it's pretty simple to understand.
All right.
Next thing that we want to do is pretty simple.
We want to add to our graph.
So we are graph right now is not that cool.
So it's pretty simple, pretty well basic.
If we want to add a little bit of style to our graph, it's possible there is a sub package of matlab
called style and we can use it.
So pretty simple.
We'll just go here and we are going.
So we need to import this sub package.
So let's do it.
Pretty simple right down from matplotlib.
So we want to import stuff so the stuff package style.
So right now we have our sub package.
What we can do right now is just add some style to our graph or a chart and we can use the style that
we want.
So basically I have four styles that I can propose you basically they're all pretty.
There are many more than four styles, but those are the four styles that are pretty simple to understand.
So the function that we will use to to add style to our graph is just the style is just a use function.
So basically we are going to make a reference to the style sub package and the use function.
So basically it's going to be style that used and then we'll write down the name of our style.
So basically, let's start with one of the styles that is called PMH, the image.
So we say this is the first style that we are going to use.
So right now we have added one style to our graph.
So if we run everything, as you can see, the graph has another set.
So basically right here, you can see there are squares.
It's more cool if we don't use style.
So basically, if we write down Classico, so it's going to be the basic the basic styles of classic
and we run it.
You'll see it's going to be a little bit different.
So basically, it's a bit different.
Everything here is great.
The style is a bit different.
There is another style that exists.
It's called dark background.
So basically we can try this one.
So it's going to be dark here background.
So back around here we go.
And we can run it.
So as you can see, this is another type of style that you guys can have.
So basically the style function will just allow you to have to give certain a certain style to your
graph.
So I guess you should be able to create your own charts with matplotlib.
So you can see it's pretty simple.
The first thing that you guys do is import matplotlib library.
Then if you want to add some style, just import the style, some package, then pretty simple, you
just add everything.
That you want a right here, so you create your you give to your X values, you give to your Y values,
then you create your functions right here.
So basically, this would be your database.
This would be where you create your function.
And finally, the last part is where you will get the title and the names to your access as well to
your graph as well.
That to your graph.
So I hope you guys understand everything.
So this are all the basics that you need to understand to be able to work with matplotlib.
So it's a first class guys into all in our next class.