plt.subplots Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 12:24:44 Hi! Everybody! Welcome back in this video, we're gonna learn about how to make subplots. 12:24:50 And Matt plot, lip. 12:24:52 So let me go ahead and open our Jupiter notebook 12:24:57 So in this notebook we're gonna see how you can place multiple axes in a single figure we're gonna learn about how the subplots works. 12:25:07 And then finally, we'll discover how you can adjust subblot placement within a figure so let's go ahead and get started. 12:25:14 So we've actually already seen an example where we put multiple axes, objects, and a single figure. 12:25:20 But now those were sort of blank. And actually we can go back. 12:25:24 Why don't we go back and check that that example out? 12:25:30 So this is what it looked like. All we had to do was use. 12:25:34 Add axes multiple from and by, adding axes. Multiple times we were able to append 3 axes, objects onto one figure. 12:25:44 But a nice thing now is that we know all these different types of charts, so we can do the same exact thing where we add axes. 12:25:52 And but now all of these axes, instead of just saying, this is an axes or axes, one axes, 2. 12:25:57 We can put nice charts in them. So, for instance, maybe what we're trying to do is eventually build some sort of charting function or application using that flat lib that will allow us to examine various data points along with like a histogram and a pyramid sort of looking at the distributions of related 12:26:14 variables, in order to allow us to both explore our data better. 12:26:19 But then also maybe make some sort of dashboard functionality for our users or our clientele, or something like that. 12:26:26 So this is a nice feature of Matt Plot lib. 12:26:30 As we can make subplots within a figure that show off different things. 12:26:35 And we can do this using the AD axes which allows us to specify the position of the the position of the axes object as well as the size of the axes. 12:26:45 Object within the figure. Another thing we can do, though, is so sometimes in in your work, you're gonna want to be. 12:26:53 You're gonna want to make a grid of axes, objects where essentially like, maybe you want to do a 3 by 2 grid of various axes, so you can make subplots to make something called maybe small multiples. 12:27:03 Plots, where you plot the same exact thing over and over, but changing one small aspect of the data to see how that impacts the pattern. 12:27:12 And this is something that people do in data visualization, because it turns out if you keep the the pattern or the shape of the data visualization the same but alter one small feature of the data set. 12:27:24 That's being plotted it's very. It's much easier for the human eye to track it and notice the differences in comparing contrast. 12:27:31 So it can be desirable to make automatically a grid of subplots. 12:27:37 And using. Add axes a alone to do that could be quite cumbersome, because, let's say, I wanted to do a 3 by 2. 12:27:43 Well, then, I have to make 6 calls to add axes, and specify by hand the positioning of those axes. 12:27:51 So there's a function in Matt Plot, lib called Plot subplots, which will automatically generate a grid of evenly spaced equally sized axes. 12:28:01 Objects as long as you provide it. How many you would like? 12:28:05 So we can call Plt. Dot sub plots 12:28:09 And then what you put inside of it is you first put the number of rows you want your grid to have. 12:28:15 So in my example, we're gonna make a 2 by 2 grid. 12:28:17 So 2 rows, and then the next thing you put is the number of columns you want. 12:28:23 So again. A 2 by 2 grid. I want 2 columns, and then I want my figure size to be maybe large. 12:28:29 So let's say, fig size is equal to, let's say 6 by 6, maybe, and then what this is gonna do is it's going to go out, make a figure, make a set of axes, arrange those axes within an array. 12:28:47 Or a list of lists, and then returning to you. 12:28:51 So the first thing that gets returned from this function is the figure object that was created the second thing that gets returned is an array of axes, objects that were placed on the figure so we can go ahead and plot something and for me, I'm just gonna put text in each grid. 12:29:09 So the axes object that is now stored in variable ax can be accessed like a twod. 12:29:15 Numpy array, or a list of lit. Well, not a list of lists, but a twod numpy array, and so I can put in ax, and if I want the plot in the upper left hand corner, I would do 0 comma 0 because it's in the 0, throw and these zeroth 12:29:31 Columns, and I'm going to put text, and then this will be axes 0 comma 0. 12:29:39 And then why don't we go ahead? We need to put a position so let's do point 4.4 5. 12:29:44 Okay. And now I can do the same thing to the upper right hand corner. 12:29:50 That's gonna be in the 0 row, and the one column text and then point 4 comma 0 point 4 5, and then again axes 0 comma one. 12:30:01 Now, if I wanted to go to the next row, that'll be ax row number one column number 0 ax one, comma 0 text point 4.4 5 axes, one comma 0, and then finally, in my 2 by grid, 2 by 2 grid, i've got axes, one comma one dot text. 12:30:24 Point 4.4 5, and then we can put a label axes, one comma one. 12:30:29 Okay, so now we've got our axes. Objects right where we want them. 12:30:35 Okay. So we've plt subplots. They created this figure, put these forward equally sized axes and they're equally spaced apart. 12:30:45 And now this is a grid. So when your grid has multiple columns and multiple rows, it gets indexed like this like a 2D numpy array. 12:30:55 If you're grid, only has one column in multiple rows, or one row. 12:31:01 And multiple columns. You index it like a oneed numpy array. 12:31:05 So here's an example where I've made 2 rows in one column, and then you index it like just a list or a 2 or a one dimensional numpy array. 12:31:15 So ax at 0, a X at one. Okay? And then here's an example with one row and 2 columns. 12:31:22 So again you index it just like a one d array a exit 0 a X at one 12:31:30 So some other things. So I've already talked about the fig size, argument, and the previous example. 12:31:38 At the very beginning I set the fig size so you can set that. 12:31:42 You can also set these arguments, share x and share y. 12:31:47 So have you set share x or share y equal to true, all of the axes within your grid will have either the same horizontal axis or the same vertical axis depending upon which one you set to true, you can set both the true but you know the share x controls the 12:32:05 horizontal share. Y controls the vertical. 12:32:08 So in this case, I'm going to create a grid where all of the axes are the same, and by axes I actually mean the horizontal and vertical, not the objects. 12:32:16 So I'm going to set share X equal to true and share. 12:32:20 Why? Equal? To? True? Okay? And now I'm gonna make the same exact plots now. 12:32:28 But the way that we're gonna be able to tell that they all have the same exact horizontal, and vertical axes is, I'm gonna set the X limits on the upper left hand corner axes to be 0 to 2 so the horizontal axis for the upper 12:32:43 Left hand corner should be from 0 to 2, and then for the upper or the lower right. 12:32:48 Hand corner. I'm going to set the vertical limits to be 0 to point 6. 12:32:53 And what we're gonna see is just by changing these 2 axes, objects, all of the other axes, objects within the grid are also changed, because I set the share X and share y equal to true. 12:33:05 Okay. And here we can see vertical axes are all 0 to point 6 horizontal axis are all 0 to 2. 12:33:13 And so this is a nice feature, because, if you remember earlier, our good was a little crowded, because we have tick marks on every single axis. 12:33:22 So if you have a grid where you suspect that the you know horizontal, vertical axes will all be the same. 12:33:29 You want to do this so that way you don't have to have all that crowding. 12:33:31 If it's just going to be repeated information. So another thing you can do with Plt making a subplot grid like this is, you can adjust the spacing. 12:33:43 So the spacing being the white space on the left of the axes, the white space on the right of the axes on the bottom, and on the top, as well as the whites space vertically between axes, objects, and the white space horizontally between axes objects we can run 12:33:58 This function. Sub plots adjust, and then put in various arguments to control that white space. 12:34:05 So the argument left controls where the axes start to be placed on the left hand side on the right controls. 12:34:12 How far they're able to go on the right hand side, left. 12:34:17 The way that this works in that plotlib is left starts at 0 and goes all the way to the one. 12:34:22 So going from left to right, we go from 0 to one. 12:34:25 0 meaning you're touching the left hand side of the figure object one meaning. 12:34:30 You're touching the right hand side of the figure objects object. 12:34:34 So one thing from that means your left argument has to be less than or equal to your right argument. 12:34:40 I put less than cause it wouldn't make sense to make it so that they were equal. 12:34:44 Similarly, your bottom starts at 0 and your top goes to one, so your bottom has to be less than your top, and then the next 2 arguments we can control are the horrors, the vertical spacing. 12:34:59 So the space between them as columns, and then the space between rows. 12:35:05 So the space between columns is controlled with. 12:35:10 Let's see H. Space, which the way I remember it is. 12:35:14 Its height, space, and I know that sounds weird. I don't know why they call that H space, but the one that controls the space between the columns is called the H. 12:35:24 Space that can be any number between 0 I think it's just can be any number. 12:35:30 And then the one that controls the space between rows is called W. 12:35:35 Space, and I maybe the way to remember that is width space. 12:35:39 So it's the spacing between the widths. I don't know. 12:35:40 I just it's a weird one to remember, and I always have to fiddle around to make sure I understand it correctly. 12:35:45 So let's say, we've got this grid. 12:35:48 So this is what the grid looks like normally, and then I'm going to use subplots. 12:35:55 Suggest you call plot subplot, adjust, and I'm let's we can just see what happens if I set the left equal to point 2. 12:36:02 So now we can it's kind of hard to tell. 12:36:05 Maybe if we can get even more extreme. Okay? And now let's see what happens if we set the right equal to point 2 12:36:18 And now we can see like if we set both the left. 12:36:20 So maybe the left we want to be point one, and the right. 12:36:23 We want to be. Point point. Let's start with just point one, and then we can combine it with the right equals. Say, point 8. Okay? 12:36:34 So we can see how it shrunk a little. The top and the bottom we can set that. 12:36:40 So what if we want the bottom to be? Point one, and then the top to be point 9 5? 12:36:49 Okay. And then finally, we can set the H space. So let's set that first. 12:36:56 Remember, this is the spacing between columns, so let's make this. 12:37:00 We can start with just point 5 alright cheese. I do it wrong again. 12:37:06 I think I did do it wrong. So the h space I'll go back and edit the notebook, but the H space is the spacing, I guess, between the rows, which makes more sense. 12:37:15 Now that I'm seeing it out loud, so we can see how this changes. Right? 12:37:20 So point, one is smaller than point 5, and then, similarly, we can set the W space which ignore all everything I said before, because it changes the spacing between the columns that makes much more sense to me. So W. 12:37:33 Space we can set this like. Let's see what happens if we set it equal to 0 point 0 1. 12:37:38 We can see that's really small, or if we set it equal to point 9. 12:37:42 Okay. So now we can see how we can control the sub. 12:37:46 The spacing between the subplots with sub plots adjust, and again, your version of the notebook. 12:37:54 This will be correct. H. Space in W. Space. It was just Detective, and I'm not going to go back and re-record it so I'll fix it, though. 12:38:03 Okay. So that's it. We have a pretty good understanding now of how to make subplots at specific positions with, add axes, and then how to make a grid of subplots with subplots. 12:38:14 Plt subplots, and then we also saw how to space around the different axes within our subplots, using the subplots, adjust command, which we'll come back to later when we talk about saving figures with Matt plot Lib so I hope you enjoyed learning about the different subplots, I