WEBVTT

1
00:00:00.200 --> 00:00:01.680
<v ->Hi guys, and welcome back.</v>

2
00:00:01.680 --> 00:00:03.400
In this video, we're going to learn how to add

3
00:00:03.400 --> 00:00:06.140
multiple axes to one figure, so that we can

4
00:00:06.140 --> 00:00:09.540
end up exporting images that have multiple graphs in them.

5
00:00:09.540 --> 00:00:10.914
Let's get started.

6
00:00:10.914 --> 00:00:14.220
Here we've got some of the code from the last video.

7
00:00:14.220 --> 00:00:16.390
We've got our figure that we're creating,

8
00:00:16.390 --> 00:00:20.350
and then we're adding a subplot, which is the axes,

9
00:00:20.350 --> 00:00:23.400
then in there, we're plotting our line chart.

10
00:00:23.400 --> 00:00:25.820
What we can also do though is we can rename

11
00:00:25.820 --> 00:00:29.720
this to, let's say, AX1 for axis one and axis two,

12
00:00:29.720 --> 00:00:31.610
and we're gonna do add subplot again.

13
00:00:31.610 --> 00:00:34.400
Now we do have two subplots in our figure,

14
00:00:34.400 --> 00:00:36.660
and we can grab that and plot something again.

15
00:00:36.660 --> 00:00:39.120
So I'm gonna plot, in this case, a slightly

16
00:00:39.120 --> 00:00:41.050
different set of numbers, one, two, three,

17
00:00:41.050 --> 00:00:43.570
four in the X axis, but five, seven,

18
00:00:43.570 --> 00:00:46.020
eleven, and seventeen in the Y axis.

19
00:00:46.020 --> 00:00:48.000
Now, if we run this, you might expect,

20
00:00:48.000 --> 00:00:49.110
since you've already seen it,

21
00:00:49.110 --> 00:00:51.320
and I've already mentioned, that you'll have

22
00:00:51.320 --> 00:00:53.720
two sets of axes in your figure,

23
00:00:53.720 --> 00:00:56.770
but that's actually not what's going to happen at all.

24
00:00:56.770 --> 00:01:00.800
Here we have one pair of axes, and two plots in it.

25
00:01:00.800 --> 00:01:01.700
So what's happened?

26
00:01:01.700 --> 00:01:05.030
Well by default, when we do figure.add subplot,

27
00:01:05.030 --> 00:01:07.590
matplotlib is going to add the subplot

28
00:01:07.590 --> 00:01:10.990
in the top left hand corner of our figure.

29
00:01:10.990 --> 00:01:13.860
Figures are essentially divided into cells.

30
00:01:13.860 --> 00:01:17.100
The top left hand corner is cell number one,

31
00:01:17.100 --> 00:01:19.290
the next one to its right is cell number two,

32
00:01:19.290 --> 00:01:20.920
the next one to its right is cell number three,

33
00:01:20.920 --> 00:01:24.060
and so on, and it goes left to right, top to bottom,

34
00:01:24.060 --> 00:01:26.640
like the diagram on the screen is currently showing.

35
00:01:26.640 --> 00:01:29.290
But here we've not told matplotlib to create

36
00:01:29.290 --> 00:01:31.210
any number of rows and columns.

37
00:01:31.210 --> 00:01:33.730
We've only told it to create different subplots.

38
00:01:33.730 --> 00:01:37.250
So by default, they're both the same subplot.

39
00:01:37.250 --> 00:01:39.700
If we want to specify different subplots,

40
00:01:39.700 --> 00:01:42.860
we can use the nomenclature of number of rows,

41
00:01:42.860 --> 00:01:47.190
comma, number of columns, comma, the index for this axes.

42
00:01:47.190 --> 00:01:51.220
So where this axes is going to appear within that grid.

43
00:01:51.220 --> 00:01:53.350
Here we're defining that this axis

44
00:01:53.350 --> 00:01:55.530
will be the top left hand corner one,

45
00:01:55.530 --> 00:01:58.170
in a grid of two columns.

46
00:01:58.170 --> 00:02:00.810
So this one is gonna be one, two, two,

47
00:02:00.810 --> 00:02:02.710
to be the one at the top right,

48
00:02:02.710 --> 00:02:04.722
in a grid of two columns.

49
00:02:04.722 --> 00:02:07.540
Now if we do this, we can run this again,

50
00:02:07.540 --> 00:02:09.800
and you see that we get two different graphs.

51
00:02:09.800 --> 00:02:11.330
Now the lines are also the same colour,

52
00:02:11.330 --> 00:02:13.270
because matplotlib is aware that these

53
00:02:13.270 --> 00:02:15.560
are two separate plots in two different places.

54
00:02:15.560 --> 00:02:17.200
The user is not gonna get confused

55
00:02:17.200 --> 00:02:19.414
between which one is which.

56
00:02:19.414 --> 00:02:23.990
When working with matplotlib, you'll often see this.

57
00:02:23.990 --> 00:02:26.620
One two one, one two two, just like that.

58
00:02:26.620 --> 00:02:28.120
It means exactly the same thing,

59
00:02:28.120 --> 00:02:30.210
but matplotlib allows us to pass

60
00:02:30.210 --> 00:02:32.510
in a single set of numbers, as apposed

61
00:02:32.510 --> 00:02:34.590
to three comma separated values.

62
00:02:34.590 --> 00:02:37.210
I really don't like this at all,

63
00:02:37.210 --> 00:02:39.520
and I would encourage you not to use it,

64
00:02:39.520 --> 00:02:41.000
but it is something that you're gonna see

65
00:02:41.000 --> 00:02:43.544
every now and then, when working with matplotlib code.

66
00:02:43.544 --> 00:02:46.560
And obviously it doesn't work when the number

67
00:02:46.560 --> 00:02:48.820
of cells reaches a number greater than nine,

68
00:02:48.820 --> 00:02:51.200
because then you need four digits instead of three.

69
00:02:51.200 --> 00:02:53.260
So generally I would recommend using

70
00:02:53.260 --> 00:02:55.750
the three number approach, with three

71
00:02:55.750 --> 00:02:59.660
different arguments, rather than passing in a single number.

72
00:02:59.660 --> 00:03:02.290
Here we have created a figure and two axes,

73
00:03:02.290 --> 00:03:04.810
and we can do this using this piece of code.

74
00:03:04.810 --> 00:03:06.910
However, there is a shorthand that we can use

75
00:03:06.910 --> 00:03:08.970
when working with multiple subplots.

76
00:03:08.970 --> 00:03:11.560
Instead of three different variables there,

77
00:03:11.560 --> 00:03:16.560
we can do figure, X1, X2, equal plt.subplots,

78
00:03:17.060 --> 00:03:19.020
and that is going to create one figure

79
00:03:19.020 --> 00:03:22.060
with two subplots, in this same structure.

80
00:03:22.060 --> 00:03:23.867
AX1 is gonna be the first one.

81
00:03:23.867 --> 00:03:25.500
AX2 is gonna be the second one.

82
00:03:25.500 --> 00:03:26.750
Note that I'm getting these errors here,

83
00:03:26.750 --> 00:03:28.210
because the variables are re-declared,

84
00:03:28.210 --> 00:03:30.510
but if we get rid of those, that goes away.

85
00:03:30.510 --> 00:03:32.790
If you start wanting to have multiple

86
00:03:32.790 --> 00:03:36.180
rows and columns, like for example two and two,

87
00:03:36.180 --> 00:03:38.350
then you're gonna start running into some problems,

88
00:03:38.350 --> 00:03:43.350
because AX1 and AX2 will now start meaning each row.

89
00:03:43.890 --> 00:03:47.074
So AX1 will be the first row, with two plots.

90
00:03:47.074 --> 00:03:49.880
X2 will be the second row, with two plots.

91
00:03:49.880 --> 00:03:53.613
So instead what we have to do is something like this.

92
00:03:56.280 --> 00:03:59.470
So here we have AX1 and AX2, and then in a separate

93
00:03:59.470 --> 00:04:01.290
tuple, AX3 and AX4.

94
00:04:01.290 --> 00:04:03.070
If you're not familiar with tuple,

95
00:04:03.070 --> 00:04:04.030
the structuring by the way, I've got

96
00:04:04.030 --> 00:04:05.990
a resource linked in the resources section

97
00:04:05.990 --> 00:04:07.520
of this lecture, for you to check out.

98
00:04:07.520 --> 00:04:09.830
What's happening here is that plt.subplots

99
00:04:09.830 --> 00:04:14.270
is returning a figure, and a tuple of tuples,

100
00:04:14.270 --> 00:04:15.950
and we are then de-structuring that

101
00:04:15.950 --> 00:04:18.560
tuple of tuples into four different variables.

102
00:04:18.560 --> 00:04:20.270
Again, if we go back to one two,

103
00:04:20.270 --> 00:04:23.660
and we do AX1 and AX2, subplots

104
00:04:23.660 --> 00:04:25.900
is now returning a tuple with figure

105
00:04:25.900 --> 00:04:28.100
and two axes, and we're de-structuring

106
00:04:28.100 --> 00:04:30.470
those two into their two variables.

107
00:04:30.470 --> 00:04:32.199
This works in exactly the same way.

108
00:04:32.199 --> 00:04:34.030
All right, that's everything for this video.

109
00:04:34.030 --> 00:04:35.360
Thank you guys for joining me on this one.

110
00:04:35.360 --> 00:04:36.550
I hope you've learned something,

111
00:04:36.550 --> 00:04:38.300
and you'll see you in the next one.

