WEBVTT 1 00:00:00.007 --> 00:00:01.005 - [Instructor] Occasionally, 2 00:00:01.005 --> 00:00:04.008 it is useful to compare different views of data 3 00:00:04.008 --> 00:00:06.004 side by side. 4 00:00:06.004 --> 00:00:09.002 This is where subplots come into play. 5 00:00:09.002 --> 00:00:12.002 Subplots are group of small axis 6 00:00:12.002 --> 00:00:15.005 that can stand together between a single figure. 7 00:00:15.005 --> 00:00:19.001 They can be in sets, grids or plots 8 00:00:19.001 --> 00:00:21.005 or some other complicated layout. 9 00:00:21.005 --> 00:00:25.006 Let's explore two ways to achieve that with functions. 10 00:00:25.006 --> 00:00:30.000 Subplot function that creates only a single subplot 11 00:00:30.000 --> 00:00:34.009 between a grid and subplots function that creates 12 00:00:34.009 --> 00:00:38.004 a full grid of subplots at once. 13 00:00:38.004 --> 00:00:41.007 Let's start with learning the subplot function. 14 00:00:41.007 --> 00:00:44.006 We have already imported "numpy as np" 15 00:00:44.006 --> 00:00:48.002 and matplotlib dot pyplot as PLT. 16 00:00:48.002 --> 00:00:51.003 We are going to define figure 17 00:00:51.003 --> 00:00:55.001 by typing fig equals PLT dot figure 18 00:00:55.001 --> 00:00:57.007 and X equals N P 19 00:00:57.007 --> 00:00:59.009 dot arange three 20 00:00:59.009 --> 00:01:05.004 and Y equals two multiplied with X. 21 00:01:05.004 --> 00:01:08.001 In order to create four subplots 22 00:01:08.001 --> 00:01:11.000 we are going to use the subplot function. 23 00:01:11.000 --> 00:01:15.009 It takes these arguments, number of rows, number of columns 24 00:01:15.009 --> 00:01:19.003 and the third argument is the index of the plot 25 00:01:19.003 --> 00:01:20.003 we're referring to. 26 00:01:20.003 --> 00:01:21.009 For the first subplot, 27 00:01:21.009 --> 00:01:24.001 we are going to have two rows, 28 00:01:24.001 --> 00:01:27.007 two columns and the plot number one. 29 00:01:27.007 --> 00:01:30.006 Next, we are going to call the plot function 30 00:01:30.006 --> 00:01:34.000 and past three arguments, X, Y 31 00:01:34.000 --> 00:01:37.008 and last we will specify plot color B. 32 00:01:37.008 --> 00:01:39.009 That stands for blue. 33 00:01:39.009 --> 00:01:43.004 We are just going to copy paste their code three times 34 00:01:43.004 --> 00:01:47.000 and change the plot number inside subplot function 35 00:01:47.000 --> 00:01:54.003 and pass different arguments in plot function. 36 00:01:54.003 --> 00:01:59.005 Now type PLT dot show. 37 00:01:59.005 --> 00:02:02.004 Go ahead and run this. 38 00:02:02.004 --> 00:02:03.003 Great! 39 00:02:03.003 --> 00:02:06.008 As you can see, we have four different subplots. 40 00:02:06.008 --> 00:02:10.003 This way of creating subplots can become tedious. 41 00:02:10.003 --> 00:02:14.001 In cases, we are creating a large grid of subplots. 42 00:02:14.001 --> 00:02:18.005 The way to do this elegantly is with the subplots function. 43 00:02:18.005 --> 00:02:23.002 We can create all four subplots with just one line of code 44 00:02:23.002 --> 00:02:24.006 by typing 45 00:02:24.006 --> 00:02:26.003 Fig comma 46 00:02:26.003 --> 00:02:27.004 access 47 00:02:27.004 --> 00:02:30.006 equals PLT dot subplots. 48 00:02:30.006 --> 00:02:33.007 Two, two fig size equals 49 00:02:33.007 --> 00:02:35.009 double six, six. 50 00:02:35.009 --> 00:02:39.006 Now notice here we still have to copy plot function 51 00:02:39.006 --> 00:02:46.009 four times in order to plot our four graphs. 52 00:02:46.009 --> 00:02:50.004 Let's run our code. 53 00:02:50.004 --> 00:02:52.005 You should see the output plots.