Hello,
I am (name) from LearnVern ( 6 Second pause ; music )
So we will be learning today in continuation with Machine Learning previous session
So, today we will proceed ahead in learning the graph Plotting by using Matplotlib.
So, how can we plot different types of graphs?
This is the same sheet that I had used, and will continue with the same sheet.
So, let us see firstly, how to create a bar graph?
Bar graph (typing)
So, to create a bar graph I will again formulate all the programmes independently.
So, that you can refer to the entire piece of code when you want to do plotting.
So, here I will write “from mat plot lib import py plot as plt”.( P L T)
Earlier we had seen to do through import matplotlib dot py plot as plt.
Hope you are able to understand.
So, we can do both the ways.
Now, here I will put some values directly in plt dot b a r, so by using bar function in plt dot bar, I will insert values in percentage over here, so small values like 0.25,second 0.30, third 0.80 and then 0.90.
So, here I have put 1,2,3,4, '4' values.After that , these are my x values meaning input data.
Now, I will put my other values which will be my y data, That is, what its height will be ? so I will make a list based on its height,
So, for 0.25 I will give height as 20,
and for 0.30 height as 30, next will be 10, the other next would be 40 and the last would be 60
So, 0.25 is 20, 0.30 is 30, 0.80 is 10 and 0.90 is 40.
So now, its enough we have till 40
Now, if you want to put some labels as well then you can do it from here itself.
So, l a b e l (label), how can we put that lets see?
For instance, we have data on population, so I will write the label is equal to p o p u l a t i o n (population), here you can pass even more parameters by putting a comma.
So, lets pass some colour parameter next, c o l o u r (colour), in this we will give it r that is red colour, and also set a width of the bar at 0.5, so, with these many parameters I am setting my one bar plot.
So, you will see this will create a bar plot.
See bar plot is created.
0.5 has become more than required,
So, I will decrease the width to 0.30, still the bar is not distinctly visible, so I will decrease it furthermore to 0.1.
The bar is ok but still overlapping, but there is no problem in it (Repeat)
Now, what is this bar displaying or interpreting is very important to know about.
Here below 0.25 and 0.30 are very close therefore this is overlapping, same is the case with 0.80 and 0.90.
So, I will change this value to 0.50 over here, and this 90,I will increase it to 1.00 here, so now this bar is distinctly visible and proper.
So, you know that this plotting is done from the Centre, so 0.25 is created from the centre, this centre will have 0.50, the centre will have point 80, and the centre will have one point zero
So, plotting is done this way from the centre, so we have already given the width as 0.1, and here height is given further, you can consider height like quantity in a way.
And in the end I will also add plt dot show.(pause six seconds , typing)
So, here you can see these values at x are my first values and at y are my second values
So, here I can show some departments over here at x and at y I can show their performances.
This is the way we can extract data between the two variables and do plotting.
So, here I will put plt dot x label is equal to or keep it in brackets so x label is equal to, these are percentages so I will write percent ( p e r c e n t ) over here and what is frequency of percent? So plt dot y label is the frequency, so I will write ‘f r e q’ which means frequency.
So, we will execute this, at the time of executing, we will be able to x label and y label here, this needs to be in brackets. And execute.
So, this is the percentage here and this is the frequency of the same.
So, this was our bar chart.
Now, let see histogram
Both histogram and bar chart look alike, Right!
So, let's see histogram.
Now, How can we use histogram?
In that we had data in the form of x and it's height.
So, when we have a lot of datas present in arrays, in a long list, then during that time we prefer using histogram.
And in this case the data falls in some sort of distribution, then we use histogram at that time.
So, to use it, let's take an example over here.
For instance, I will take an example of population or let's take age as an example, as to what are different ages and make a normal list of it.
Now, somebody's age is 12 years, somebody else's age is 13 years, some is 32 years, the other is 56 years, 78 years, 90 years, 43 years.
So all these are ages. Ok?
7:56
Next concept is bins, similar to the height that we had used previously.
So what happens in bins is i have to mention the span such as starting from until the ending point.
Like 0 to 10,10 to 20, 20 to 30, 30 to 40.
So, i have to mention bins and it will check accordingly, like from 0-10, there is no value from zero to ten. so it will not display that,but if I did from 10-20, so 12, 13 will be lying between 10-20, so it will show its frequency count that there are 2 which will come between them.
So, let's take up bins over here, which are 0, then 10, then 20, then 30, thereafter I will directly take 100.
Ok, so I made a large bin.
So, we have taken age and bins, on whose basis we will plot now.
So, plt dot hist will be used for histogram so in plt dot hist we will put age for x and then we will put bins equal to b i n s (bins).
Along with that if you want to add anything else fancy, you can add that as well , so the minimum things I have put over here. Ok!
So, here I will type h i s t type( mention histtype), so histogram type will be equal to bar type, here we can mention the width as well r width let's take it as 0.8. ok?
So, let's execute this and see what it shows.
So, you can see that from 0-10 we had only two values, so it is showing only two over here and then from 10 to 20, we had one value, so it is showing only one.. rest I have taken all in one bin
So, it shows frequency, such as how many people are 22 yrs, how many people are 23 yrs of age, so this shows frequency. If we take whole range instead of it, that how many people are there in between 20 to 25 years, so this way we have taken ranges,how many people are in between 0 to 10 years of age,how many are in between 10 to 20 and between 20 to 30 and after that between 30 to 100…So, these were our histogram, in which bins are used..
Next, we will learn about the scatter plot which is most popularly used.
You must have seen scatter plots in many analyses.
So, scatter plots are used to show relationships, like I was explaining about correlation earlier.
So, to show the relationship , how can we show the relation?
So to show the relation, let's take x and y again. From the same examples that I had created earlier, I will take x and y from there.
So, this is my x and y, i have picked, I will copy and paste it here,
Now let's give them some logical name to it, like w e i g h t, so this will become weight and here let's name it as speed.( S p e e d)
So, normally as the weight increases the speed decreases, so I will flip the values or the data over here so first would be 6km/per hour speed, because the weight is less, then 5 , then 4 , then 3, then 2, then 1. the number of elements are 1, 2, 3, 4 above, its till 8.
So after one here, 0.5.
and add 7 over here, so this is speed. So we made an array of weight and speed.
Now on this array we will do plotting.
So, here in the start we have forgotten to put bracket, so this is bracket, In the bracket, our values have been started from 7 , let's add 8 one more value at the start, so let's see (1,8), (2,7), (3,6), (4,5), (5,4)
(6,3)( 7,2), (8,1) ok.
So, this value has become proper now. (pause 4 seconds ; typing )
So, we have got weights and speed.
Now, for scatter plot plt dot s c a t t e r(scatter)
So, plt dot scatter this is the command for it and here you can simply put weights and s p e e d(speed) and enter.
And this will give you .
See it has displayed the data.
So, this is how it has given us the plotting.
So,here you can see it is negatively correlated.
As the weights keep increasing it will result in a decrease of the speed.
So, we can see a negative correlation over here.
So, this is a scatter plot where you can find correlations.
Now, let's look at area plots or it is also known as stat plots.
It is similar to a line plot, but the difference is that the area below the line becomes shaded.
So, these are similar to line plots.
Let's see!
For area plot, we had already written width and speed so we will reuse the same again.
So, to plot this we will keep the width and speed, so here plt dot plot, in that we will keep two frames, and thereafter here I will keep c o l o u r (colour).
So, colour is equal to here we will take green so, g.
Then we will give l a b e l(label) here.
So, one label would be weight and the other would be speed,
So, label is equal to w e i g h t(weights)
After this do we require anything else?
Ok, we will take a line width, l i n e w i d t h(linewidth), so we will give a value to this line width, so here we will give a value 5.
In the same way here, as we did it for weight similarly plt dot p l o t (plot), and here again we will create two frames for x and y, and here we can give some other colour. So let's give it the red colour as earlier we had given green, so c o l o r (colour) is equal to r.
So, let's give this red.(pause 4 seconds)
So, the first one was weight so we will give this as speed, so label, here will be l a b e l is equal to speed, Ok!
So, here also we are supposed to give line width, we will give it as 5, so we have also given line width here.
Now, we will plot this , so we will use plt dot s t a c k (stack) to plot, here we will use stack plot. So in the stack plot whatever that we insert we want to do in it, so we have only weight and speed, and we don't have anything more, so let's take age also over here.
So, I will make an array for age also, so np dot a r r a y (array) here. Ok so in this array, 1, 2, 3, 4, 5, 6, 7, 8 ok I have 8 elements so I will insert 8 elements over here, such as 12 years, 32 years, 34 years, 54 years, 67 years, so 1, 2,3,4,5 ok we have given 5 values so next 6, 7 ,8.
So we have 8 values here.
Fine!
So, here in plt dot stackplot what we are going to do over here is as we put x and y first, similarly we will insert w e i g h t s (weights), then s p e e d (speed) and then one more we will add that is age.Ok!
So, we have added three parameters over here, weight, speed and age.
So, because we have to stack, that is to show one upon the other, so we can take more than 2 parameters for it, as in 2 we will get a single diagram only, where there will be no possibility of stacking one upon the other. Hence we took these many things like weight, speed and age.
Now, we can directly create a plot but we can also give colours to it.
C o l o r(colour), so we will give blueish and then green.
In this way.
So, let us plot this.
I am not writing x label or y label.
We have green and red colours.
In this you can see the green portion is covering most of the area ok! as compared to blue which has very less space.
So, this is how it helps to understand who is covering more area and who is covering less.
So, this we are able to do through stack plotting.
Ok! So let me show you one more chart. This is a pie chart .
So, the pie chart we use to depict in percentage.
So, supposingly if I write here percentages for sales as 12 percent, 13 percent, so when added we should get 100, so I will take a cell here 12+ 13+ 29+30 , what is the sum? it's sum becomes 84, ok! So, we will do one more plus over here and insert a value that is 26.
Valuable came to be 110 so let’s keep it 16.
So, this is our 100 values in total.
So, here we will take now 29 after 13, then 30 and at last we will take 16.
This is the way,Ok!
So, this is the way we received the sales data of every department happening overall.
So, let's name this DEPT, department, equal to first is 'It', second t e c h(tech), third 'hr' related, 1, 2, 3, 4, 5, fourth 'admin' related, and fifth is related to 'sales'.
So, here I have created a list.
Now, you can give any colours that you want, let’s specify already, for instance first will be blueish, second green ,third blue colour, 1, 2, 3, 4, 5, we have 5 colours, mention the colours that you want to give. next m is a keyword and we have ‘r’ as a key word
So, 1, 2, 3, 4, 5, fine.
we have defined the colours over here.
So, in this way you can define whatever you want in the plot before.
So, as soon as you make a function call , we will pass all these things over there, so that these functions can do plotting according to this.
So, let's do plt dot pie, here in the bracket we have to give x which is sales, next label is equal to dept (department), and colours is equal to c.
Now, let's execute and see how our pie chart looks, “sales not defined” ok so here we didn't execute the sales.
So, let's execute this also.
So, you can see that it has visualised all the departments hr, tech, it, sales, admin, looking at it we understand that admin department has the maximum ratio.
Which interprets the admin department has high sales value.
So, these were the five different types of visualisation that we saw in graph plotting with matplotlib.
So, friends, let's conclude today, we will stop our session today.
We will study further topics in the next session.
Till then keep learning and remain motivated.
If you have any questions related to this course or you have any comments.
then you can click on the discussion button below this video and post it there.
So, in this way you can discuss this course with many other learners of your kind.
Share a personalized message with your friends.