WEBVTT

1
00:00:00.150 --> 00:00:01.460
<v ->Hi guys and welcome back.</v>

2
00:00:01.460 --> 00:00:03.180
In this video, we're going to talk about

3
00:00:03.180 --> 00:00:05.680
very important concepts in matplotlib,

4
00:00:05.680 --> 00:00:08.610
the figure, the axes and the plot.

5
00:00:08.610 --> 00:00:10.080
In the last couple of videos, we've been

6
00:00:10.080 --> 00:00:13.470
using them, but it's not been terribly clear what is what.

7
00:00:13.470 --> 00:00:15.910
So we're gonna make that clear in this video.

8
00:00:15.910 --> 00:00:17.380
Let's get to it.

9
00:00:17.380 --> 00:00:20.300
The figure is the top level container.

10
00:00:20.300 --> 00:00:22.110
Inside a figure, we can include one

11
00:00:22.110 --> 00:00:24.770
or more axes, and inside each axes,

12
00:00:24.770 --> 00:00:26.966
we can have one or more plots.

13
00:00:26.966 --> 00:00:28.810
I'm going to get rid of almost

14
00:00:28.810 --> 00:00:30.880
all this code here, and just leave

15
00:00:30.880 --> 00:00:34.600
the figure and the show, and I'm gonna run this file.

16
00:00:34.600 --> 00:00:36.370
You can see that what we get here

17
00:00:36.370 --> 00:00:39.640
is basically an empty file with nothing inside it.

18
00:00:39.640 --> 00:00:42.520
There's no axes, and there's nothing plotted.

19
00:00:42.520 --> 00:00:44.750
We've used pyplot to create the new figure,

20
00:00:44.750 --> 00:00:47.440
and when we do this, pyplot automatically

21
00:00:47.440 --> 00:00:48.920
selects the figure, and that becomes

22
00:00:48.920 --> 00:00:51.280
the currently active figure.

23
00:00:51.280 --> 00:00:54.730
And when we do plt.show, that is the figure

24
00:00:54.730 --> 00:00:56.033
that we're showing.

25
00:00:56.033 --> 00:00:58.170
Later on, we're going to learn how to assign

26
00:00:58.170 --> 00:00:59.790
these two variables, so that we can show

27
00:00:59.790 --> 00:01:01.650
specific figures when we want to,

28
00:01:01.650 --> 00:01:04.100
but for now, that's how it works behind the scenes.

29
00:01:04.100 --> 00:01:06.310
Every other function in the pyplot library

30
00:01:06.310 --> 00:01:08.640
is going to act on the currently selected figure,

31
00:01:08.640 --> 00:01:11.950
after it is created, until a different one is selected.

32
00:01:11.950 --> 00:01:14.650
Let's now add a set of axes to the figure.

33
00:01:14.650 --> 00:01:17.089
The axes, which is a matplotlib class,

34
00:01:17.089 --> 00:01:18.890
is where plots are drawn.

35
00:01:18.890 --> 00:01:20.900
You can have multiple plots drawn

36
00:01:20.900 --> 00:01:23.140
on a single pair of axes, or you can also

37
00:01:23.140 --> 00:01:25.500
have multiple axes in one figure.

38
00:01:25.500 --> 00:01:27.020
Let's start off by adding a set

39
00:01:27.020 --> 00:01:29.200
of two dimensional axes to our figure.

40
00:01:29.200 --> 00:01:31.250
We can do plt.axes, and now what

41
00:01:31.250 --> 00:01:33.310
this is going to do is create

42
00:01:33.310 --> 00:01:36.580
a pair of axes in the currently selected figure,

43
00:01:36.580 --> 00:01:38.240
which is the one we just created.

44
00:01:38.240 --> 00:01:41.599
Here we can pass in a list of 06020,

45
00:01:41.599 --> 00:01:44.440
for example, and this means that our X axis

46
00:01:44.440 --> 00:01:46.510
is gonna go from zero to six,

47
00:01:46.510 --> 00:01:49.780
and our Y axis is gonna go from zero to 20.

48
00:01:49.780 --> 00:01:51.980
Now if we run this, you'll see that

49
00:01:51.980 --> 00:01:54.220
we get our axes here.

50
00:01:54.220 --> 00:01:56.920
Matplotlib tries to create ticks evenly

51
00:01:56.920 --> 00:01:59.926
throughout the limits, so that we get 2.5,

52
00:01:59.926 --> 00:02:02.360
five, 7.5, and so on, even though we only

53
00:02:02.360 --> 00:02:05.400
defined zero and 20 as our limits.

54
00:02:05.400 --> 00:02:06.740
At the moment though, we've got nothing

55
00:02:06.740 --> 00:02:09.290
drawn in the canvas, so let's change that.

56
00:02:09.290 --> 00:02:12.140
Plots can be drawn on a set of axes,

57
00:02:12.140 --> 00:02:14.680
and each plot needs to specify data points

58
00:02:14.680 --> 00:02:16.700
for the axis that it is drawn on.

59
00:02:16.700 --> 00:02:19.130
So for example, if we have a two dimensional axes,

60
00:02:19.130 --> 00:02:21.540
as we do here, then we need to provide

61
00:02:21.540 --> 00:02:25.310
the X and Y coordinates for each point that we want to draw.

62
00:02:25.310 --> 00:02:27.570
That's why earlier on, when we used plt.plot,

63
00:02:27.570 --> 00:02:30.610
we provided a list of X coordinates,

64
00:02:30.610 --> 00:02:32.690
and a list of Y coordinates,

65
00:02:32.690 --> 00:02:35.860
and these will then map to the axis.

66
00:02:35.860 --> 00:02:37.160
If we run this though, you'll see that

67
00:02:37.160 --> 00:02:39.120
we've got a small problem.

68
00:02:39.120 --> 00:02:40.920
The data points that we've drawn,

69
00:02:40.920 --> 00:02:44.700
one, two, three and four, as well as three,

70
00:02:44.700 --> 00:02:48.290
five, nine and 25, don't exactly fit

71
00:02:48.290 --> 00:02:50.210
within the axes that we've created.

72
00:02:50.210 --> 00:02:52.590
That's why four and 25 gets partially

73
00:02:52.590 --> 00:02:54.940
clipped here going up, because there's not

74
00:02:54.940 --> 00:02:56.870
enough room in our axes for it.

75
00:02:56.870 --> 00:02:59.120
When we created our line chart earlier on,

76
00:02:59.120 --> 00:03:02.070
we didn't use plt.axis to create our axes,

77
00:03:02.070 --> 00:03:04.450
and therefore matplotlib created them for us,

78
00:03:04.450 --> 00:03:06.550
making sure that the plot fit inside

79
00:03:06.550 --> 00:03:08.120
the axes in its entirety.

80
00:03:08.120 --> 00:03:10.720
So you should only use plt.axis when you want

81
00:03:10.720 --> 00:03:14.300
to specifically define your axes limits to certain numbers.

82
00:03:14.300 --> 00:03:16.090
If we run this without that, you'll see

83
00:03:16.090 --> 00:03:17.670
that the plot now fits exactly.

84
00:03:17.670 --> 00:03:20.280
But you can also see that the distribution

85
00:03:20.280 --> 00:03:22.130
of our ticks has changed.

86
00:03:22.130 --> 00:03:23.560
All right, so just to recap,

87
00:03:23.560 --> 00:03:25.920
what we've got here is a figure.

88
00:03:25.920 --> 00:03:27.620
Inside the figure, we can have

89
00:03:27.620 --> 00:03:29.720
one or more sets of axes.

90
00:03:29.720 --> 00:03:32.120
In this case, we have one set of axes.

91
00:03:32.120 --> 00:03:36.090
And inside each axes, we can have one or more plots.

92
00:03:36.090 --> 00:03:37.790
Well in fact, we can have zero or more plots,

93
00:03:37.790 --> 00:03:39.130
if you wanna leave them empty.

94
00:03:39.130 --> 00:03:42.130
Here we've got one plot in our set

95
00:03:42.130 --> 00:03:44.280
of axes, inside our figure.

96
00:03:44.280 --> 00:03:47.210
And as show, we can also have multiple axes,

97
00:03:47.210 --> 00:03:49.430
and they go into rows and columns,

98
00:03:49.430 --> 00:03:51.450
and we can also have multiple plots within each.

99
00:03:51.450 --> 00:03:52.890
Thank you guys for joining me in this video.

100
00:03:52.890 --> 00:03:53.960
I hope you've learned something,

101
00:03:53.960 --> 00:03:55.610
and I'll see you in the next one.

