So, today we will continue from our previous session of Machine learning.
Now, we are going to learn about Graph Plotting with matplotlib.
Before visualising data or before applying any algorithm, we visualise that data graphically.
Graphical visualisation is really helpful for us because it helps to understand beforehand only as to what kind of distribution pattern the data falls into, or if there are any outliers in it.
So, these are things that we understand through graphical representation.
So, over here python matplotlib is a very low level and a fantastic utility that helps in graph plotting.
Let's see how we can import this and work ahead on it.
So, to import this you can directly write import matplotlib. In import matplotlib, the package that we usually use in this is pyplot, so import matplotlib dot py plot as PLT, so we are going to use this package mostly,
And if you haven't installed it, then you can install it through the process that I have explained before also, which is pip install matplotlib.
However, this is by default pre-installed in almost all environments in most cases.
So, in this way you can Install it and use it.
Now, I will Import this, if you want to know about its version, you can simply write print and over here mention matplotlib matplotlib dot, now it's not showing me any suggestions because I haven't imported matplotlib separately.
So I will Import it separately over here, now I have imported it separately also.
Now print matplotlib dot underscore underscore V E R S I O N version.
So, with this I will understand the version of matplotlib that is 3 point 2 point 2.
So I am using this particular version right now.
Let’s move ahead and begin the topic.
Here we have already imported matplotlib dot pyplot as plt, here we will create a sample data and see how we can make graph plotting out of it.
So, here at first I will create some X data points over here.
So to make X data points it's better if we import numpy, so import…numpy as NP, However we could also take 'list' but we know that numpy arrays are more efficient when compared with 'list'.
So, X is equal to NP dot A R R A Y array, so let's make an array over here and for Array, we will make a list from 0 to 10. So we made the list from 0 to 10 & we created a list from 0 comma 10
And for Y, Y is equal to the NP dot array and here also I will pass a list starting from 0, both are starting from 0 but we will end it at 300.
So, here are X and Y as my two arrays, this is my X array and this is my Y array.
Now, we will do plotting for this.
So, let's plot.
That means we are going to create a graph for which we will start with a basic PLT dot plot, and in this plot we will insert X and Y, then you can write PLT dot show which is optional to write.
We will do PLT dot show.
So, here we will see a plot, so it has made a line chart over here, so the first coordinate of X is 0,0 and the other coordinate is 10 & 300
So, you can see here one plotting is done here at 0,0
And on 10 & 300 the other plotting is done
Therefore, in this way it has represented a particular data graphically for us.
So, we will move ahead and see it's different ways of plotting and functions,
So, now we will extend this example,
Supposingly if I want to represent some labels over here. How can we represent this?
Taking the same example I will copy paste this quote over here.
So I want the labels over here (typing pause 4 sec) for which I already have plt dot plot but along with that I will also add plt dot X label and pass whatever I want to see on the X axis, so as I have 1,2,3,
I would like to see F R E Q frequency over here.
And on, Y axis I will mention plt dot Y label and pass 'count' over here
So, here frequency will be written here, and count will be written here.
And now I will print this.
You can see here on X axis it's written frequency over here, and Y axis there is count.
Now, let's move ahead
Labels are here, and along with label,
And now I don't just want to see lines on my graph but want to see some shapes
So, I will take this quote ahead and write down labels and would like to see two points so I will add that, as we don't have anything much in our X and Y except for these two points that are 0,0 and 10,300.
So, when I am using this plot function PLT plot, along with that there itself I can define what kind of shape I want.
So, here I am defining that I want a shape which looks like O, and then execute this.
Now, you can see the shape which I mentioned is drawn over here.
This is the way by which we can change the shapes as well.
Now, we will move ahead.
Uptil now I only have two points,so I will increase the number of points.
For which I will make an initialization again
X is equal to NP dot array and here I will pass such a list which will have more than 2 elements, so 1,2,3,4,5,6,7,8.
So, here I have passed a total of 8 elements.
Similarly for Y, here NP dot array and pass a list having 2,4,6,8,10,12,14,16.
I hope we have completed 8 elements.
So, here I will execute this.
And here, we will see X dot S H A P E shape, in that we have 8 elements.
Similarly for Y, Y dot S H A P E shape, in that also we can see we have 8 elements.
So, in both we have the same shape, as you know that shape should be the same otherwise it will give a mismatch and would lead to an error instead of plotting as there will be places where values will be less and not in pairs which will give errors.
Because of this we have given same shape to both the points
So, again for plotting Plt dot plot and insert X and Y and now you can see here that as we have put the plotting, the line is increasing in linear fashion and at the same pace.
We cannot clearly understand if the plotting is done in this line or not.
So, we do the same action as we previously did and change the shape.
Now you can see it's showcasing us plotting at each point.
So, let's move ahead and see another example.
If we have a single array supposingly I have taken only X and Y, so here X represents something and Y represents something else.
For instance X represents salary and Y represents number of work experience, or skill set.
So X and Y represent two different things, where, when we execute plotting we understand if there is any correlation between them.
So, here we can make out that both the coordinates are highly correlated as X is increasing similarly Y increases at the same pace, so both are highly correlated.
But if we change this, and give here X is equal to 1,2,3,4,5 and on Y we give 4,4,4,4,4 a constant value.
And then we plot this, plt dot plot, you would understand that what kind of graph we will get, it will have a constant line on 4, not above it nor lower to it, because when X is 1 still Y is 4, when X is 2 Y is still 4 and till the end the X value is changing but Y remains the same.
So, be it any increment or decrement in X there is no affect on it over here the value always remains the same.
So, there is no correlation between them, as there is no change happening on Y because of X.
So, we can understand that if we plot the data this way it will help us to increase our understanding, our insights, our awareness of the data more.
Now, If I am having a single variable, we cannot do any pairing in it as we could do with XY, so what can be done in those cases.
Let us see that.
For instance I have Y only , so Y is equal to NP dot array and here I will create a list and give values 1,2,3,4,5,6,7,8.
These are the values.
Now, I have to plot this.
To plot this we will be helped by the same function, right? That is, plt dot P L O T plot and here I will press Y and enter.
So, here you can see it has displayed those values which I had given here.
Now, you might be thinking that I am taking the same value again and again, so I will vary the values so that you can notice the a change in the graph
So, here I have changed the values
And now let us execute this
Now, here you can see our plotting has changed into something like this, with a single value or variable , so we can plot a graph even with a single value or variable.
So, down here it has automatically chosen 1,2,3,4 these values for it on the X axis.
And on Y it has used the actual figure for it.
So, let's move ahead and further explore it
And here let us see about markers.
So, this diagram that we saw here is a marker.
So, this we will change with a marker which is the name of the actual argument or name of the parameter.
So, in the same way I will use plt dot plot and in this I will be writing X and Y. But I just need Y so I will only write down Y marker, but we cannot see this keyword over here, but still let us try and give a shape marker that is equal to we will put O.
So, now you can see it is working, and it has given us the same line but along with that it is also showing us the circular or the dotted points as well.
So, this is helping us to understand the trend and also helps to know the distinct sets of the points on the data as well.
So, this is one of the benefits that we are getting with markers.
So, let us move ahead, here you should remember that whenever you want to change the marker you can do that referring to the documentation and get the list of marker from it
After that you can specify the marker you want to use here, so you can see we got a star here.
So there are many different types of markers such as circle, star,pixels or we can show X,X signs or plus plus signs.
So, now let us move ahead along with the marker. We can do so many things with the marker. You can also change the format of this line by using an F M T parameter.
That helps in multiple arguments such as the colour of the line, the type of line, the type of marker you want.
So, let us use the FMT parameter as well,
For which our command will be same plt dot plot and in that we will insert just Y, after that now we will add FMT in it, but it's ok even to not mention FMT, but let us try with this, so…FMT is equal to here we will give in single quotes O and R.
It is not able to recognise the FMT like this.
Hence, I will remove this from here, and just go with the parameter only.
So, here I got the changes as dotted lines and the colour is red, if you want to change the colour in blue you can do that.
Now, you can see your colour has changed to blue.
Do you understand what I’m saying?
So, the first one over here is for the shape that you want to keep, second one is the type of line that you want to select and the third one is for the colour that you want to use.
So, this is the way we can modify our plotting and whenever we do multiple plotting then this type of diagram helps us.
This also has many colours that you can check.
Now, I want to add one more thing over here, so I want to increase the size of the plus sign as it is very small and not easily visible, so I will change the size of the marker used.
It is called MS so, for that MS is equal to size will be 15, and then execute this.
So, let's see what is the change in the size, so you can see the size has updated and changed over here.
Now, again I will change the marker and use a circle over here, so again we got a change in the diagram over here.
Now, I would like to highlight the circle over here.
So for that we will add, so we have Y over here, O B, size ,ok!
So along with size we will mention a colour so, MEC. We will be applying some colour with the MEC so, is equal to I will give red colour over here as we already have blue.
Here you can see it has given an outer boundary in red colour over here.
So, this is the way by which we can give colouring also.
So, there are many more small features of that sort that can be used to set the plot.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Now, moving ahead we will see different types of shapes.
So, till then keep watching and remain motivated
Ruturaj Nivas Patil
Very well explained in entire course. Great course for everyone as it takes from scratch to advance level.