1 00:00:00,060 --> 00:00:06,000 Welcome back to our class of all, of course, about the complete introduction to data science with 2 00:00:06,000 --> 00:00:06,580 Python. 3 00:00:07,170 --> 00:00:13,590 So in this class, we are going to talk about matplotlib and learn how to create a basic matplotlib 4 00:00:13,590 --> 00:00:18,020 graph or a chart and modify this graph or chart. 5 00:00:18,050 --> 00:00:23,950 So basically, this is one of the best ways that you guys can use to visualize data inside of Python. 6 00:00:24,780 --> 00:00:29,160 So to be able to do all this, a divided discourse and fourth by four parts. 7 00:00:29,190 --> 00:00:32,550 So basically the first part we are going to learn to import matplotlib. 8 00:00:32,850 --> 00:00:36,350 So basically works pretty much the same way as Banda's, no worse Kibuye. 9 00:00:36,390 --> 00:00:42,000 So things that we talked about in the past few classes, then we are going to create a really simple 10 00:00:42,000 --> 00:00:46,740 graph, really create something basic so you guys will see what it all looks like. 11 00:00:47,640 --> 00:00:52,440 Then I'll show you a more advanced way to be able to write everything down. 12 00:00:52,470 --> 00:00:55,570 So basically how usually we write down function. 13 00:00:55,610 --> 00:01:02,760 So this way we are able to create not only one line inside of our graph will not only integrate one 14 00:01:02,760 --> 00:01:06,690 function of our graph, but have like four or five, six functions inside of our graph. 15 00:01:06,690 --> 00:01:09,040 So basically more than one X in one way. 16 00:01:09,060 --> 00:01:17,130 So you can have an infinity of Xs and OS, and then after that we are going to add titles, labels and 17 00:01:17,130 --> 00:01:17,500 everything. 18 00:01:17,520 --> 00:01:22,490 And finally, we are going to change the style of our graph and make it more beautiful. 19 00:01:22,500 --> 00:01:25,450 So we are going to talk about all this inside of this class. 20 00:01:26,160 --> 00:01:27,250 So let's start. 21 00:01:27,570 --> 00:01:30,150 So basically, the first thing that we are going to do is pretty simple. 22 00:01:30,150 --> 00:01:35,190 We want to import matplotlib to be able to work with this Python library work package. 23 00:01:35,520 --> 00:01:37,030 And how exactly do we import it? 24 00:01:37,050 --> 00:01:37,790 It's pretty simple. 25 00:01:37,800 --> 00:01:44,940 So we will write down import and want to import as Matt Plup Lib Dot Bayport. 26 00:01:46,680 --> 00:01:49,230 So we want to import all this as VLT. 27 00:01:49,260 --> 00:01:54,690 So basically field you can write down anything that you want, but Kielty is the usual, the usual name 28 00:01:54,690 --> 00:01:56,550 of the pipeline. 29 00:01:57,330 --> 00:01:57,690 All right. 30 00:01:57,700 --> 00:02:03,720 So if you don't write this, if you don't write down this line of code, normally nothing would work 31 00:02:03,720 --> 00:02:05,470 because you didn't import matplotlib. 32 00:02:06,720 --> 00:02:12,060 So when everything is ready, what we will do right now, we are going to create a basic graph with 33 00:02:12,060 --> 00:02:13,880 just one function inside of it. 34 00:02:14,280 --> 00:02:15,900 So basically it's going to be one line. 35 00:02:15,900 --> 00:02:18,450 So we'll have one X and one white. 36 00:02:18,990 --> 00:02:20,670 So let's write it down. 37 00:02:21,000 --> 00:02:21,660 So pretty simple. 38 00:02:21,670 --> 00:02:23,710 The first thing that will do right on field. 39 00:02:23,940 --> 00:02:29,280 So we are making a reference to matplotlib by writing down Kielty and the function that we are going 40 00:02:29,280 --> 00:02:31,570 to use will be blood. 41 00:02:31,980 --> 00:02:39,690 So basically Bluck right here and then we are going to write Xs here and our Y's here. 42 00:02:40,830 --> 00:02:44,460 So for the Xs, let's say it's going to be one, three and five. 43 00:02:45,690 --> 00:02:53,020 And for the guys, let's say it's going to be five, ten and twenty five. 44 00:02:53,850 --> 00:02:55,410 So basically we have just two numbers. 45 00:02:55,410 --> 00:02:57,500 If you guys want to add more, add more numbers. 46 00:02:57,510 --> 00:02:58,230 Let's say five. 47 00:02:58,230 --> 00:02:59,710 We have 10 in here. 48 00:02:59,730 --> 00:03:01,830 Let's say it's going to be one hundred and twenty five. 49 00:03:02,460 --> 00:03:02,840 All right. 50 00:03:02,880 --> 00:03:05,310 So basically, we have our numbers right here. 51 00:03:05,310 --> 00:03:10,320 But if we click on this little thing, it's not going to work because we didn't wrote down the function 52 00:03:10,590 --> 00:03:13,000 that will allow us to print everything right here. 53 00:03:13,470 --> 00:03:18,530 So to be able to see this graph, what we'll need to use is the show function. 54 00:03:18,540 --> 00:03:20,580 So it's going to be Kielty that show. 55 00:03:21,120 --> 00:03:25,770 And we want to see this what exactly we have just created. 56 00:03:27,330 --> 00:03:28,200 So pretty simple. 57 00:03:28,350 --> 00:03:33,210 You just write down to that show and you will be able to see the graph that you guys have just created. 58 00:03:33,240 --> 00:03:35,810 So basically we can compare to what we see right here. 59 00:03:36,150 --> 00:03:39,420 So we have one in here is going to be one four five. 60 00:03:40,560 --> 00:03:43,560 So with five is right here, then we'll have three. 61 00:03:43,740 --> 00:03:46,240 So three will be somewhere right here for ten. 62 00:03:46,680 --> 00:03:49,740 Then we'll have five for twenty five. 63 00:03:49,770 --> 00:03:51,470 So basically five would be somewhere here. 64 00:03:51,870 --> 00:03:53,420 So four twenty five makes sense. 65 00:03:53,430 --> 00:03:57,670 And finally ten for one hundred and twenty five which makes sense as well. 66 00:03:58,080 --> 00:04:00,570 So can see this is the graph that we have just created. 67 00:04:00,600 --> 00:04:05,320 So if we put our cursor right here, you can see down there everything is fine. 68 00:04:05,610 --> 00:04:08,200 So basically this is the first grab that we have created. 69 00:04:08,700 --> 00:04:09,110 All right. 70 00:04:09,390 --> 00:04:11,880 Right now, let's say that you guys want to have more than one line. 71 00:04:11,910 --> 00:04:16,090 So if for those who don't understand what I'm saying, so this would be one function. 72 00:04:16,110 --> 00:04:17,380 So this is just one function. 73 00:04:17,730 --> 00:04:19,950 Let's say you guys want to have more than one function. 74 00:04:20,460 --> 00:04:22,700 How exactly you can do all this? 75 00:04:23,760 --> 00:04:25,380 So it's pretty simple. 76 00:04:25,800 --> 00:04:27,340 There is another way to write it down. 77 00:04:27,360 --> 00:04:29,790 Usually we don't write that this way. 78 00:04:29,800 --> 00:04:32,300 So basically, this is just a presentation way. 79 00:04:32,320 --> 00:04:35,070 So just to present one chart or one graph. 80 00:04:35,880 --> 00:04:38,720 So the way that we are going to write everything down will be pretty simple. 81 00:04:38,730 --> 00:04:41,950 So we need our X's and our way. 82 00:04:41,970 --> 00:04:43,110 So basically X 83 00:04:45,840 --> 00:04:48,650 right there and are wise. 84 00:04:48,870 --> 00:04:51,080 So let's say we have to function. 85 00:04:51,090 --> 00:04:56,040 So it's going to be X and X one, Y one. 86 00:04:56,040 --> 00:04:59,520 So we'll have X to and Y to. 87 00:05:01,100 --> 00:05:07,780 So let's write it down, so if we're X1, what we'll do right now, we'll just add something. 88 00:05:07,790 --> 00:05:10,320 So let's say, for example, we want to add what we want. 89 00:05:10,380 --> 00:05:12,430 I will give them numbers. 90 00:05:12,770 --> 00:05:17,360 So it's going to be, let's say, two, four and eight and four. 91 00:05:17,360 --> 00:05:25,880 Why no one is going to be for eight and twenty four, for example, for the X number two is going to 92 00:05:25,880 --> 00:05:27,220 be pretty much the same thing. 93 00:05:27,230 --> 00:05:34,820 So let's say it's going to be three, six, nine and four y number two is going to be in this case, 94 00:05:34,820 --> 00:05:40,210 let's say 10, 15 and 30. 95 00:05:40,700 --> 00:05:47,840 Let's keep it like this so you can see the first thing that we will do usually will be write down our 96 00:05:47,840 --> 00:05:49,100 will want to write down. 97 00:05:49,130 --> 00:05:51,280 So basically all our database will be here. 98 00:05:51,620 --> 00:05:58,250 So all the X values and the Y values, then next thing would be writing down the plot, that plot function, 99 00:05:58,610 --> 00:06:00,590 for example, to the plot. 100 00:06:02,720 --> 00:06:12,140 And then what we want to do is show everything so that plot will have X and Y and find in the second 101 00:06:12,140 --> 00:06:19,690 one will be plot that blood X to Y two. 102 00:06:20,240 --> 00:06:25,480 So basically the the plot function will simply be the function that will allow us to work with that, 103 00:06:25,490 --> 00:06:26,820 let's say, for example, X and Y. 104 00:06:26,840 --> 00:06:32,780 So basically we want to work with X1 and Y one and we'll just write down X right here and wait right 105 00:06:32,780 --> 00:06:33,010 here. 106 00:06:33,020 --> 00:06:35,810 Same thing right there from the work with X2 and Y two. 107 00:06:36,140 --> 00:06:38,120 I'll just write down X2 and wait two. 108 00:06:38,570 --> 00:06:41,870 And finally, the last thing that we want to do is just show everything. 109 00:06:41,880 --> 00:06:47,120 So we'll write down that show and we can run our up. 110 00:06:47,150 --> 00:06:51,950 So basically what's going to happen is that we'll see are two lines right here so we can compare if 111 00:06:51,950 --> 00:06:53,050 it's what we have written. 112 00:06:53,420 --> 00:06:55,100 So this is exactly what we have written. 113 00:06:55,100 --> 00:06:57,050 So basically we have our first line right here. 114 00:06:57,050 --> 00:06:59,090 So four X one and Y one. 115 00:06:59,480 --> 00:07:02,270 It's blue and four x two, Y two. 116 00:07:02,270 --> 00:07:02,930 It's right there. 117 00:07:02,930 --> 00:07:09,230 If I change, for example, my thing right here instead of X to Y two, I just write down X and Y two. 118 00:07:09,710 --> 00:07:11,210 You'll see it's going to be a bit different. 119 00:07:12,650 --> 00:07:17,660 Because you can see became parallel because I'm working with two exes that are the same. 120 00:07:18,200 --> 00:07:23,570 So once again, this is more in mathematics, but as you can see, it works pretty well to be able to 121 00:07:23,570 --> 00:07:24,410 generate a graph. 122 00:07:24,920 --> 00:07:25,300 All right. 123 00:07:25,310 --> 00:07:31,430 So right now that we have our function, next thing that we want to do is write down, give a title 124 00:07:31,430 --> 00:07:32,980 to our graph. 125 00:07:33,230 --> 00:07:39,950 And not only this, we want to give a value to X and Y value to Y. 126 00:07:39,980 --> 00:07:43,500 So basically a title for X and it's a little too wide. 127 00:07:44,090 --> 00:07:46,640 So how exactly do we write all this down? 128 00:07:46,670 --> 00:07:47,450 It's pretty simple. 129 00:07:48,380 --> 00:07:51,390 So we'll just write down that title. 130 00:07:51,920 --> 00:07:54,130 So this will allow us to generate a title. 131 00:07:54,620 --> 00:07:57,920 So let's call it out on the list. 132 00:07:58,010 --> 00:08:00,100 Let's call it desk. 133 00:08:01,310 --> 00:08:06,730 So the title of our graph will be Test and then X values. 134 00:08:06,740 --> 00:08:07,700 We'll call it. 135 00:08:08,390 --> 00:08:10,190 Let's call it test X. 136 00:08:11,390 --> 00:08:15,490 So here we'll have built that X label. 137 00:08:16,100 --> 00:08:22,200 So basically we are giving a name to X labels, to the X, to the X value. 138 00:08:22,230 --> 00:08:36,320 So basically we'll call it, let's call it, as I said, test X values and plotty dot Y labels, which 139 00:08:36,320 --> 00:08:42,090 would be the name of the white labels would be test y values. 140 00:08:42,090 --> 00:08:45,290 So test y values. 141 00:08:46,100 --> 00:08:46,500 All right. 142 00:08:46,520 --> 00:08:51,550 So what I have just done right here, I just give a name to my access. 143 00:08:51,560 --> 00:08:56,080 So basically this would be the name of my axis and this would be the title of my chart. 144 00:08:56,510 --> 00:08:59,840 So everything what I did right now is pretty simple. 145 00:08:59,840 --> 00:09:04,090 So I still have the same, the same functions right here. 146 00:09:04,100 --> 00:09:05,480 So my functions are still the same. 147 00:09:05,780 --> 00:09:09,550 I just added a title and I added a axis name. 148 00:09:09,920 --> 00:09:15,260 So if I run everything, as you can see here, I have my chart name. 149 00:09:15,280 --> 00:09:23,630 So basically test, then I have text a test y values, which is my wife values and test X values, which 150 00:09:23,630 --> 00:09:25,430 are all my X values. 151 00:09:25,670 --> 00:09:28,380 So you can see it's pretty simple to understand. 152 00:09:28,640 --> 00:09:29,040 All right. 153 00:09:29,080 --> 00:09:30,980 Next thing that we want to do is pretty simple. 154 00:09:30,980 --> 00:09:32,900 We want to add to our graph. 155 00:09:32,900 --> 00:09:36,290 So we are graph right now is not that cool. 156 00:09:36,780 --> 00:09:40,610 So it's pretty simple, pretty well basic. 157 00:09:40,940 --> 00:09:48,320 If we want to add a little bit of style to our graph, it's possible there is a sub package of matlab 158 00:09:48,320 --> 00:09:51,200 called style and we can use it. 159 00:09:51,590 --> 00:09:52,670 So pretty simple. 160 00:09:52,670 --> 00:09:55,760 We'll just go here and we are going. 161 00:09:55,760 --> 00:09:58,280 So we need to import this sub package. 162 00:09:58,940 --> 00:09:59,960 So let's do it. 163 00:09:59,960 --> 00:10:03,860 Pretty simple right down from matplotlib. 164 00:10:03,860 --> 00:10:11,360 So we want to import stuff so the stuff package style. 165 00:10:13,100 --> 00:10:15,050 So right now we have our sub package. 166 00:10:15,410 --> 00:10:22,880 What we can do right now is just add some style to our graph or a chart and we can use the style that 167 00:10:22,880 --> 00:10:23,160 we want. 168 00:10:23,180 --> 00:10:27,730 So basically I have four styles that I can propose you basically they're all pretty. 169 00:10:28,220 --> 00:10:32,990 There are many more than four styles, but those are the four styles that are pretty simple to understand. 170 00:10:33,810 --> 00:10:40,290 So the function that we will use to to add style to our graph is just the style is just a use function. 171 00:10:40,310 --> 00:10:45,710 So basically we are going to make a reference to the style sub package and the use function. 172 00:10:45,740 --> 00:10:50,180 So basically it's going to be style that used and then we'll write down the name of our style. 173 00:10:50,180 --> 00:10:55,990 So basically, let's start with one of the styles that is called PMH, the image. 174 00:10:56,600 --> 00:10:59,180 So we say this is the first style that we are going to use. 175 00:11:00,170 --> 00:11:03,410 So right now we have added one style to our graph. 176 00:11:03,510 --> 00:11:08,570 So if we run everything, as you can see, the graph has another set. 177 00:11:08,610 --> 00:11:11,000 So basically right here, you can see there are squares. 178 00:11:11,270 --> 00:11:14,100 It's more cool if we don't use style. 179 00:11:14,120 --> 00:11:18,980 So basically, if we write down Classico, so it's going to be the basic the basic styles of classic 180 00:11:20,810 --> 00:11:21,590 and we run it. 181 00:11:22,460 --> 00:11:24,060 You'll see it's going to be a little bit different. 182 00:11:24,080 --> 00:11:25,630 So basically, it's a bit different. 183 00:11:25,640 --> 00:11:26,990 Everything here is great. 184 00:11:27,710 --> 00:11:29,120 The style is a bit different. 185 00:11:30,080 --> 00:11:31,670 There is another style that exists. 186 00:11:31,670 --> 00:11:33,100 It's called dark background. 187 00:11:33,110 --> 00:11:35,030 So basically we can try this one. 188 00:11:35,540 --> 00:11:38,990 So it's going to be dark here background. 189 00:11:38,990 --> 00:11:42,200 So back around here we go. 190 00:11:42,200 --> 00:11:43,220 And we can run it. 191 00:11:43,690 --> 00:11:46,670 So as you can see, this is another type of style that you guys can have. 192 00:11:47,000 --> 00:11:53,960 So basically the style function will just allow you to have to give certain a certain style to your 193 00:11:53,960 --> 00:11:54,310 graph. 194 00:11:54,590 --> 00:11:59,420 So I guess you should be able to create your own charts with matplotlib. 195 00:11:59,420 --> 00:12:00,590 So you can see it's pretty simple. 196 00:12:00,600 --> 00:12:04,780 The first thing that you guys do is import matplotlib library. 197 00:12:05,360 --> 00:12:10,970 Then if you want to add some style, just import the style, some package, then pretty simple, you 198 00:12:10,970 --> 00:12:11,930 just add everything. 199 00:12:11,990 --> 00:12:17,990 That you want a right here, so you create your you give to your X values, you give to your Y values, 200 00:12:17,990 --> 00:12:19,820 then you create your functions right here. 201 00:12:19,850 --> 00:12:21,520 So basically, this would be your database. 202 00:12:21,890 --> 00:12:24,030 This would be where you create your function. 203 00:12:24,050 --> 00:12:30,590 And finally, the last part is where you will get the title and the names to your access as well to 204 00:12:30,590 --> 00:12:31,670 your graph as well. 205 00:12:31,670 --> 00:12:32,610 That to your graph. 206 00:12:33,170 --> 00:12:34,750 So I hope you guys understand everything. 207 00:12:34,760 --> 00:12:39,530 So this are all the basics that you need to understand to be able to work with matplotlib. 208 00:12:39,770 --> 00:12:42,860 So it's a first class guys into all in our next class.