Transcript:

Hello, guys, welcome to my Youtube channel and today. In session with Sumit, we will look at how we can work with. The subplot functionality provided with matplotlib package. Now it happens multiple time when you are trying to plot multiple subplots, uh, you you find very difficult to work with for loops and to to automate the process of plotting multiple plots, so lets. Try to look at a dom. A demonstration which will help you to, uh, do this process easily. Okay, so what we can do first of all, you can see that I have one file, which is known as telco churned or CSV, so I’ll import pandas and I will also import and I will import Matplotlib Pi plot as PLT, So let’s run it now. Let’s load the file PD. Dot read, underscore CSV as so. My file name is telco underscore John Dot csv, so DF Dot head. So this is my data that I have okay now. Let’s say I wanted to plot. Uh, the subplots for all the categorical variables with respect to the churn, So let let’s first of all do it without the follow. So let’s say I wanted to. I wanted to find the count of, uh, the partner. So how many, uh, or maybe let’s say dependent, so how many dependent as no belongs to churn, uh, churn is equal to yes, and how many yes belongs to churn is equal to No. So let me do it, and then it will make more sense. What, I’m trying to say so subplot, so what we will do? We will first of all use the subplot functionality, Uh, subplot and here we need to mention the number of rows, number of columns and then the plot number, so this particular function pltsubplot it takes the total number of row’s, comma total number of columns and the plot number. So, uh, if this is not making sense to you, let me just show you a very good example. Let’s say I wanted to plot 5 plots, something like this, So in this particular case. The number of rows will be three. So this is my row number one. This is my row number two, and this is my row number three, so I have three row and the number of columns will be two columns, so column number one column number two. So I have so pl T dot subplot. I have, uh, three rows, two columns now for this particular plot. Which is this one? This will be plot number one. This will be plot number two. This will be plot number three. This will be plot number four. This is plot number five, then. Remember one thing that you can’t have plot number zero? So this number starts always from one, Okay, so, um, yeah, so lets. Do this! Let me comment this completely and let me write a new code Plt dot subplot, so I wanted to plot two subplots side by side, so I will have one row and two columns, So one comma two. And this is my plot number. One now let’s take one of the columns, so I will. I will use the, um, SNS dot count plot. Now in this I will have, um, let’s say senior city or maybe partner. So column name is partner. Oh, sorry, partner P Capital. And I can now mention U is equal to, uh, my target column is John. So U is equal to churn, and finally your data, so data is equal to DF. So this is my plot number. One, let’s run it and check. Oh, sorry, I don’t have C bond package, so let’s import C bond as SNS. It’s done now. Let’s check this, so I have one plot, right, Similarly, I can write Plt Dot subplot for the for the next plot, So this will be one comma Two comma two so again, this thing will remain constant. Only the plot number will change. So this is plot number one. This is plot number two. Okay, so I will again copy the same code, but instead of partner, I may take some other column dependence. Maybe, so let’s put it over here. So now you will be having two plots, okay. Uh, there is some issue. Oh, sorry, sorry, sorry. This is one comma, Two comma two. So now you have two plots side by side. You can also change this shape as the figure size, so plt dot figure fig size is equal to 10 comma, or maybe 5 comma, 10 5 is my breadth 10 is my height, so height is so much, so let it put five five comma five, and let’s put it this ten, so width is ten breath is five. So now you have a clear two clear plots. Okay, now, if I if you wanted to do this thing. Uh, without writing this code multiple times. But using the the the for loop the way you can do it. So for that particular thing, first of all, you need to filter out the name of the columns, so lets. Check the column names that I have, and let’s say I wanted to work with, so let me create a feature list. So for my feature list, I will have all these columns, lets. Take all these columns, and I will remove this one. Because this is a continuous variable and apart from this, everything is a is a categorical variable. So this is my feature metric. Uh, this is a feature list. Now what I will do I will. I will write a function that is that is enumerate. Now, first of all, let’s look at what this enumerate will do. So this enumerate will return you. So let me use the list functionalities, so that we can look at the output, so it will return you the, uh, the the tuple with the index number and the column name so index number zero column numbers, gender index number. One column name is senior citizen and so on so total, we have 16 Columns is starting from 0 to 0 to 15 So now what I will do, I will write a follow for I. In enumerate enumerate features features. Now what I will do, I will write the subplot functionality first of all now in the subplot. I need to mention the number of rows, so we have 16 columns, so if you wanted to have three plots in one row, so in that case, you will be having three fives of 15 then one more, so you will be having six rows, so if you wanted to have four plots, then it in case you’ll be having four rows, so let’s keep the three plots in one row. So in that case, I have six rows six comma. How many columns so three plots in each row? So this means three columns then? This is my plot number one. So plot number one what I can use. I can use this number, so I will say I of 0 now. What is I I will be this double. I will be this tuple and I of 0 will be this number now. Remember as I have told you that this that the plot number cannot be zero. So in that case, I will add one one to it so that this number can be converted to one and this can be converted to, and so on now once you have this subplot functionality, you can write the actual plot functionality, which is basically your this count plot now in this count plot. You need to remove this part and you need to add I of 1 now. What is I? I is basically this particular tuple. This is I of 0 and this is I of 1 so here. We are providing the column name. Okay, now, let’s run this, okay. Uh, I have not, I have not so. This is feature and this is features. Okay, so it will take some time you can see all the plots are there, but it is very much in a bad shape, so let’s change the figure size plt dot figure, then fig size is equal to. Lets let’s keep the the breadth as 15 and the height as 30. So it will again. You can see that you have all the plots in a very clear way. Now you can see that there are some problems with these names. These labels so how you can overcome this problem. This is that is again very simple. You can use this PLT DOT X text function and you can rotate your labels. The X labels so rotation is equal to 90 degree, or maybe 45 degree, so as you can see, all the labels are rotated 45 degree, and you can see this label is also very clear. Okay, so, guys, this is how we can, uh, make use of the subplot functionality whenever you are trying to plot so many plots and using the for loop you can you make use of this code? This code will be available in my description box, so you can download it from here there and don’t forget to comment. Uh, what you think about this video? And what you think about my work. Please don’t forget to subscribe this my channel and please share with your friends. And those who are currently working in the data science field and those who have started working in the data science field. So my videos will be very helpful for them till the next video. I say goodbye, thank you.